"pickle" vs. f.write()
Martin Miller
marmille at gmail.com
Sat Feb 5 21:45:28 EST 2005
Marc 'BlackJack' Rintsch wrote:
> ...
>
> 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.
Yes, that's an excellent suggestion and improvement.
Saw the following in a post by Steven Bethard on another thread
<http://groups-beta.google.com/group/comp.lang.python/msg/ed6b6ba0346e3c80>
that I think would be even better (in the sense of being more
general/generic) which also ought to work in subclasses. Namely:
def __repr__(self):
return '%s(%s)' % (self.__class__.__name__,
', '.join('%s=%r' % (k, v)
for k, v
in self.__dict__.items()))
Martin
More information about the Python-list
mailing list