If I try to print the class variable which is a list, I get a Python object. (These are examples I found on stackoverflow).
class Contacts:
all_contacts = []
def __init__(self, name, email):
self.name = name
self.email = email
Contacts.all_contacts.append(self)
def __str__(self):
return '%s, <%s>' % (self.name, self.email)
c1 = Contacts("Grace", "[email protected]")
print(c1.all_contacts)
[<__main__.Contact object at 0x0287E430>, <__main__.Contact object`
But in this more simple example, it actually prints:
class Example():
samplelist= [1,2,3]
test= Example()
print (test.samplelist)
[1, 2, 3]
I figured this line is the culprit: Contact.all_contacts.append(self)
in the first sample code. But I'm not exactly sure whats going on here.
EDIT:
Several users told me to just append self.name
instead of just self
.
So when I do this:
class Contacts:
all_contacts = []
def __init__(self, name, email):
self.name = name
self.email = email
Contacts.all_contacts.append(self.name)
Contacts.all_contacts.append(self.email)
def __str__(self):
return '%s, <%s>' % (self.name, self.email)
def __repr__(self):
return str(self)
c1 = Contacts("Paul", "[email protected]")
c2 = Contacts("Darren", "[email protected]")
c3 = Contacts("Jennie", "[email protected]")
print(Contacts.all_contacts)
I get:
['Paul', '[email protected]', 'Darren', '[email protected]', 'Jennie', '[email protected]']
Instead of:
[Paul, <[email protected]>, Darren, <[email protected]>, Jennie, <[email protected]>]
Thus, the formatting in the __str__
method isn't working here.
Use Python's vars() to Print an Object's Attributes The dir() function, as shown above, prints all of the attributes of a Python object. Let's say you only wanted to print the object's instance attributes as well as their values, we can use the vars() function.
Python __repr__() function returns the object representation in string format. This method is called when repr() function is invoked on the object. If possible, the string returned should be a valid Python expression that can be used to reconstruct the object again.
The __str__ method in Python represents the class objects as a string – it can be used for classes. The __str__ method should be defined in a way that is easy to read and outputs all the members of the class. This method is also used as a debugging tool when the members of a class need to be checked.
When you print a list, it calls the __str__
for the list, but list internally calls __repr__()
for its element. You should implement the __repr__()
for your class as well. Example -
class Contacts:
all_contacts = []
def __init__(self, name, email):
self.name = name
self.email = email
Contacts.all_contacts.append(self)
def __str__(self):
return '%s, <%s>' % (self.name, self.email)
def __repr__(self):
return str(self)
Demo -
class Contacts:
all_contacts = []
def __init__(self, name, email):
self.name = name
self.email = email
Contacts.all_contacts.append(self)
def __str__(self):
return '%s, <%s>' % (self.name, self.email)
def __repr__(self):
return str(self)
contact1 = Contacts("Grace1", "[email protected]")
contact2 = Contacts("Grace2", "[email protected]")
contact3 = Contacts("Grace3", "[email protected]")
print(Contacts.all_contacts)
Result -
[Grace1, <[email protected]>, Grace2, <[email protected]>, Grace3, <[email protected]>]
Also, from the output it would seem like the list actually has 6
elements, so you should consider changing that the __repr__
returns.
Your code constructs a list of objects when it says:
Contacts.all_contacts.append(self)
because self is an object, and gets appended to the all_contacts list..
If you want something different, append something different to the all_contacts list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With