"pickle" vs. f.write()

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Wed Feb 2 12:37:03 EST 2005


In <41FF70A5.8070806 at tx3.com>, Martin Miller wrote:

> I've found extending this property to your own classes often fairly easy
> to implement (and useful). For example:
> 
>> class person:
>>     def __init__(self, name="", age=0, friends=None, comment=""):
>>         if friends is None:
>>             friends = []
>>         self.name, self.age, self.friends, self.comment = name, age, friends, comment
>> 
>>     def __repr__(self):
>>         return ("person(" + repr(self.name) + ", " + repr(self.age) + ", " +
>>                 repr(self.friends) +  ", " + repr(self.comment) + ")")

I write __repr__() methods similar but I think a bit more readable:

  def __repr__(self):
      return "%s(%r, %r, %r, %r)" % (self.__class__.__name__, self.name,
                                     self.age, self.friends, self.comment)

And it's robust against changing the class name.  It even works in
subclasses if the signature of the __init__() method stays the same.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list