Bug in New Style Classes

Michael Hudson mwh at python.net
Thu Jun 17 07:05:58 EDT 2004


michele.simionato at poste.it (Michele Simionato) writes:

> David MacQuigg <dmq at gain.com> wrote in message news:<rmu1d09qiqtosgdq1vavv3736sb62bktri at 4ax.com>...
> > I have what looks like a bug trying to generate new style classes with
> > a factory function.
> > 
> > class Animal(object): pass
> > class Mammal(Animal): pass
> > 
> > def newAnimal(bases=(Animal,), dict={}):
> >     class C(object): pass
> >     C.__bases__ = bases
> >     dict['_count'] = 0
> >     C.__dict__ = dict
> >     return C
> > 
> > Canine = newAnimal((Mammal,))
> > TypeError: __bases__ assignment: 'Mammal' deallocator differs from
> > 'object'
> > 
> > If I remove the 'object' from the class C(object) statement, then I
> > get a different, equally puzzling error message:
> > 
> > TypeError: __bases__ items must be classes
> > 
> > The function works only if I remove 'object' from all base classes.
> > 
> > -- Dave
> 
> This is not a bug. The developers removed the possibility to change
> the bases of a new-style class. 

Bad news for you: I put it back in for 2.3.

If you read the error message, you'll notice that it's phrased to
suggest that assignment to __bases__ is *sometimes* possible :-)

David's assignment probably should work -- there's a bug on sf about
this -- but there are definitely situations where assignment to bases
*shouldn't* be allowed -- e.g. when the so-called 'solid base' changes
-- but noone's put in the thinking time to make this precise in code.
Being over-restrictive seems the better course.

However, newAnimal could be written like this:

def newAnimal(bases=(Animal,), ns=None):
    if ns is None:
        ns = {}
    ns['_count'] = 0
    return type('C', bases, ns)

which 

a) doesn't use the name of a builtin as a variable
b) doesn't suffer the 'mutable default arguments' problem
c) is rather less insane
d) actually works :-) (probably, haven't tested it)

Cheers,
mwh

-- 
  Academic politics is the most vicious and bitter form of politics,
  because the stakes are so low.                      -- Wallace Sayre



More information about the Python-list mailing list