how do I use the __metaclass__ variable to change the default base class?

Michael Hudson mwh at python.net
Mon Dec 2 10:45:23 EST 2002


Gonçalo Rodrigues <op73418 at mail.telepac.pt> writes:

> You want to change the bases of a class, not the class's class (the
> metaclass) - two different things (once again: read the descintro
> carefully). Presently you cannot do that directly, e.g.:
> 
> >>> class test(object):
> ... 	pass
> ... 
> >>> test.__bases__
> (<type 'object'>,)
> >>> test.__bases__ = str
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in ?
> TypeError: readonly attribute
> 
> I believe work is being done on a patch by M. Hudson for 2.3 to
> allow this.

It's checked in now, but the example you give won't work:

>>> class test(object):
...  pass
... 
>>> test.__bases__ = (str,)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: __bases__ assignment: 'str' deallocator differs from 'object'

But if you stick to things inheriting from object, it works as you'd
hope.

Cheers,
M.

-- 
  But since I'm not trying to impress  anybody in The Software Big
  Top, I'd rather walk the wire using a big pole, a safety harness,
  a net, and with the wire not more than 3 feet off the ground.
                                   -- Grant Griffin, comp.lang.python



More information about the Python-list mailing list