Style guide for subclassing built-in types?

Fuzzyman fuzzyman at gmail.com
Wed Feb 23 07:10:36 EST 2005


janeaustine50 at hotmail.com wrote:
> Fuzzyman wrote:
> > I guess print is using the __repr__ (or __str__ ?) methods of lsit
-
> > which you will need to override as well.
> >
> > Regards,
> >
> > Fuzzy
> > http://www.voidspace.org.uk/python/index.shtml
>
> Thank you but the problem is that I have to express my intention in
> duplicate places -- __iter__(along with "next"), __str__, __eq__ and
so
> on.
>

If you are sublassing the built in types  I guess you will have to
change all methods that have changed..

an alternative wuld be to not subclass object and override __getattr__
:

class rev_wrap:
    def __init__(self,l):
        self.l=l
    def __getitem__(self,i):
        return self.l[-i-1]
    def __getattr__(self, attr):
        return getattr(self.l, attr)

Regards,

Fuzzy
http://www.voidspace.org.uk/python/index.shtml

> p.s. the reason I'm not sticking to reversed or even reverse :
suppose
> the size of the list is huge.




More information about the Python-list mailing list