Rich __repr__

Ben Finney bignose+hates-spam at benfinney.id.au
Wed Nov 2 00:17:35 CET 2005


Marc 'BlackJack' Rintsch <bj_666 at gmx.net> wrote:
> In <dk4hai$euj$1 at rose.polar.local>, Ben Finney wrote:
> 
> >     class Human_Sex(str):
> >         def __repr__(self):
> >             repr_str = "%s(name=%s)" % (
> >                 self.__class__.__name__,
> >                 str.__repr__(self)
> >             )
> >             return repr_str
> 
> I'm a bit surprised that `Human_Sex` is subclassing `str`.

So far, the "attribute" classes in this application are just
state-encapsulations, so they might as well subclass 'object'. I
subclassed 'str' to get the constructor and __str__ cheaply (in coding
time). The rest of the baggage of 'str' is rather unnecessary, I
suppose.

-- 
 \    "Anything that is too stupid to be spoken is sung."  -- Voltaire |
  `\                                                                   |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list