ZCJ
ZCJ

Reputation: 499

Python list in a function in a class

I'm trying to print the list created by the functions in this class- what do I need to fix? I'm getting output from the terminal along the lines of [<__main__.Person instance at 0x1004a0320>,.

class Person:
    def __init__(self,first,last,id,email):
        self.firstName=first
        self.lastName=last
        self.id=id
        self.email=email
        self.friends=[]
    def add_friend(self,friend):
        if len(self.friends)<5:
            self.friends.append(friend)
        if len(friend.friends)<5:
            friend.friends.append(self)

p1=Person("David","Waver","922-43-9873","[email protected]")
p2=Person("Bob","Jones","902-38-9973","[email protected]")
p3=Person("James","Smith","302-38-9103","[email protected]")
p4=Person("Tim","Jack","902-38-0918","[email protected]")
p5=Person("Jim","Johnston","314-78-2343","[email protected]")
p6=Person("Gina","Relent","102-38-1064","[email protected]")
p7=Person("Greg","Morris","932-38-4473","[email protected]")

p1.add_friend(p2)
p1.add_friend(p3)
p1.add_friend(p4)
p1.add_friend(p5)
p1.add_friend(p6)
p1.add_friend(p7)

print p1.friends

Upvotes: 2

Views: 2700

Answers (3)

senderle
senderle

Reputation: 150977

You need to define __repr__ or __str__ in your Person class.

>>> class Person:
...     def __init__(self,first,last,id,email):
...         self.firstName=first
...         self.lastName=last
...         self.id=id
...         self.email=email
...         self.friends=[]
...     def add_friend(self,friend):
...         if len(self.friends)<5:
...             self.friends.append(friend)
...         if len(friend.friends)<5:
...             friend.friends.append(self)
...     def __repr__(self):
...         return self.firstName + ' ' + self.lastName

Then initialize the list as above...

>>> print p1.friends
[Bob Jones, James Smith, Tim Jack, Jim Johnston, Gina Relent]

This answer gives a good explanation of these functions.

Given the above post's point about the functions of __repr__ and __str__, probably the __repr__ should look more like this:

def __repr__(self):
    template = "Person('{0}', '{1}', '{2}', '{3}')"
    return template.format(self.firstName, self.lastName, self.id, self.email)

What's nice about the above is that it generates a string that, when evaluated, creates an object that has the same properties (apart from friends) as the original. For example:

>>> print p1
Person('David', 'Waver', '922-43-9873', '[email protected]')
>>> Person('David', 'Waver', '922-43-9873', '[email protected]')
Person('David', 'Waver', '922-43-9873', '[email protected]')

Upvotes: 7

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

The representation of an object is given by the string returned by its __repr__() method. The string shown when the object itself is printed is the string returned from its __str__() method. Frameworks may use the string returned by the __unicode__() method for displaying the object.

class Person:
   ...
  def __repr__(self):
    return 'Person: %s, %s' % (self.lastName, self.firstName)

Upvotes: 3

Praveen Gollakota
Praveen Gollakota

Reputation: 38940

You need to define a __repr__ method for your class. For example, like this

class Person:
    def __repr__(self):
        return '%s, %s, %s, %s' %(self.firstName, self.lastName, self.id, 
                                  self.email)

Upvotes: 1

Related Questions