Joe Doe
Joe Doe

Reputation: 193

Calling instance of a class in Python

Hi I am trying to call instance of a my class but don't know how to do that, at this stage it only works if I pre specify everything in newContact and ignores class AddContact. I would like to be able to new contacts to the list maybe? Please help.

My code:

 class People():
    def __init__(self, name, surname, age, mobile_no, home_no):
        self.name = name
        self.surname = surname
        self.age = age
        self.mobile_no = mobile_no
        self.home_no = home_no

    def DisplayContacts(self):
        print("First Name: \t", self.name)
        print("Surname: \t", self.surname)
        print("Age: \t", self.age)
        print("Mobile Number: \t", self.mobile_no)
        print("Home Number: \t", self.home_no)

class AddContact():

        newname = str(input("First name: \t"))
        newsurname = str(input("Surname: \t"))
        newage = int(input("Age: \t"))
        newmobile_no = int(input("Mobile Number: \t"))
        newhome_no = int(input("Home Number: \t"))
        newContact = People(newname, newsurname, newage, newmobile_no, newhome_no) 


newContact = People()  

for p in newContact():
    p.DisplayContacts()

Upvotes: 0

Views: 800

Answers (3)

Dan Lenski
Dan Lenski

Reputation: 79828

The basic problem here is that you're assuming the People class will act as a container type to hold multiple contacts, whereas you've written as a class which will hold only the attributes of a single contact.

The actual goal of your code is unclear. You switch back and forth between treating the People class as a single contact and as a collection thereof:

In the __init__ function you prepare the object to store a single contact (one person with one name, age, etc.)

class People():
    def __init__(self, name, surname, age, mobile_no, home_no):
        self.name = name
        self.surname = surname
        self.age = age
        self.mobile_no = mobile_no
        self.home_no = home_no

This function, although it is named DisplayContacts (plural), also treats the object as storing a single contact:

  def DisplayContacts(self):
      print("First Name: \t", self.name)
      print("Surname: \t", self.surname)
      print("Age: \t", self.age)
      print("Mobile Number: \t", self.mobile_no)
      print("Home Number: \t", self.home_no)

I don't know quite what this is. I think you mean to create a function rather than a class here, so I'm changing it as such. Now you read in a new contact's information and store it in a new instance of the People class:

# was class AddContact(), which makes no sense:
def AddContact():

        newname = str(input("First name: \t"))
        newsurname = str(input("Surname: \t"))
        newage = int(input("Age: \t"))
        newmobile_no = int(input("Mobile Number: \t"))
        newhome_no = int(input("Home Number: \t"))
        newContact = People(newname, newsurname, newage, newmobile_no, newhome_no) 

Now for the main program: you create a single contact by instantiating the oddly-named People class. Then you try to call this object as a function (???). This will cause an error because you haven't defined a __call__ method but it probably isn't what you meant to do in the first place. Finally, you try to use that result as an iterator. I don't know what the intention is:

newContact = People()  

# will cause an error: no People.__call__ method
result_of_calling_object_like_a_function = newContact()

# would cause another error, unless the object in question has an
# __iter__ method:
for p in result_of_calling_object_like_a_function:
    p.DisplayContacts()

Upvotes: 1

Savenkov Alexey
Savenkov Alexey

Reputation: 698

I think you want to create a list of class instances.

You can find out how to create lists in Python here.

And you can find out what class is here.

Here is an overview of classes in Python.

Upvotes: 0

huu
huu

Reputation: 7482

If you want to build a list of contacts, instead of AddContact as a class, make it a function:

def add_contact():
    newname = str(input("First name: \t"))
    newsurname = str(input("Surname: \t"))
    newage = int(input("Age: \t"))
    newmobile_no = int(input("Mobile Number: \t"))
    newhome_no = int(input("Home Number: \t"))
    newContact = People(newname, newsurname, newage, newmobile_no, newhome_no)
    return newContact

Then, you can create a list of contacts:

contacts = []
contacts.append(add_contact())
contacts.append(add_contact())

And finally, display the contacts in the list by iterating over it:

for p in contacts:
    p.DisplayContacts()

Upvotes: 1

Related Questions