OO-programming question

Alex Martelli aleaxit at yahoo.com
Mon Sep 4 18:18:44 CEST 2000


"Rainer Deyke" <root at rainerdeyke.com> wrote in message
news:dJBs5.122703$6y5.82311962 at news2.rdc2.tx.home.com...
> "Alex Martelli" <aleaxit at yahoo.com> wrote in message
> news:8ou9i4029q1 at news2.newsguy.com...
> > def allbases(aclass, baselist=[]):
> >     baselist.append(aclass)
> >     bases=aclass.__bases__
> >     for base in bases:
> >         allbases(base, baselist)
> >     return baselist
> >
> > def allclasses(aninstance):
> >     return allbases(aninstance.__class__)
>
> This will break the second time allclasses is called.  Don't use mutable
> default args unless you really know what you're doing.

Ooops, you're right -- the baselist is never cleared when
a call of allbases from allclasses is finished, but rather
stays around, as 'state' of the allbases function, and
keeps being appended to.

Removing the '=[]' from the definition of allbases, and
changing the body of allclasses to:

    return allbases(aninstance.__class__,[])

fixes the bug, of course.


Alex






More information about the Python-list mailing list