0

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()
2
  • 2
    newContact is a "Person", the name "People" is confusing as it is not a collection. Commented Jun 17, 2014 at 19:17
  • 1
    Among other issues is your terminology: "call [an] instance of a <s>my</s> class." Calling an instance of a class would mean using the instance like a function, which is something you can do in Python by implementing a __call__ method, but doesn't appear to be what you're trying to do here. Commented Jun 17, 2014 at 19:22

3 Answers 3

1

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()
Sign up to request clarification or add additional context in comments.

2 Comments

I have done that but it did not help for some reason
Can you explain what "some reason" means? We can't help you unless you give us more information on what exactly your issue is.
1

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()

5 Comments

Yes, People class supposed to be container for many contacts, but how can I make it to store multiple contacts?
@Dan, upvoted for relevance to why the author's design is poor and how it could be improved.
@JoeDoe, Python already has excellent built-in support for container types. The best object design in your case is to have a Person class, and use something like a list to contain multiple Person instances.
Python already has container classes like list and dict which can store arbitrary objects. You could rename the original class to Person and create another class named People which would use a list to contain multiple Person objects.
What @Huu Nguyen said :). I see no evidence that you need a custom container class here.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.