Q: Meta-class usage

Michael Hudson mwh at python.net
Fri Dec 12 16:29:57 CET 2003


google at daishi.fastmail.fm (daishi) writes:

> Hi,
> 
> I am wondering if someone could help me understand some of the
> details of using metaclasses.
> 
> I am trying to use metaclasses to auto-generate some attributes
> and methods in a class definition. I am attaching a simplified
> example of the kind of code I am considering below, and I have
> the following questions:
> 
> 1. Is the code in NamedType.__new__ the "right" way to have a
> class which has NamedType as its metaclass inherit from
> NamedClass?

Apart from the fact that tuple(list(bases)+[NamedClass]) is a strange
way of spelling bases + (NamedClass,), yes.

> Are the semantics different from if the class with NamedType as its
> metaclass explicitly listed NamedClass as a superclass in the
> standard way? 

Well, potentially, depending on where in the bases list you put
NamedClass.  In general, it might be wise to not add NamedClass to the
bases list unless you need to (to avoid MRO calculation errors).

> 2. Is the try/except clause in NameType.__init__ the "right" way to
> prevent KeyErrors for subclasses of classes with NamedType as its
> metaclass?

I'd say so, but I'm not totally sure what you're trying to achieve...

Cheers,
mwh

-- 
  The Internet is full.  Go away.
                      -- http://www.disobey.com/devilshat/ds011101.htm




More information about the Python-list mailing list