In <[EMAIL PROTECTED]>, 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
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to