creating many similar properties

James Stroud jstroud at mbi.ucla.edu
Wed Oct 18 18:54:05 EDT 2006


Michele Simionato wrote:
> George Sakkis wrote:
> 
>>Why is this less hidden or magical than a metaclass ?
> 
> 
> Because it does not use inheritance. It is not going to create
> properties on subclasses without
> you noticing it. Also, metaclasses are brittle: try to use them with
> __slots__, or with non-standard
> classes (i.e. extensions classes), or try to use multiple metaclasses.
> I wrote a paper
> about metaclasses abuses which should be published soon or later; you
> can see the draft
> here:
> http://www.phyast.pitt.edu/~micheles/python/classinitializer.html

I am in the midst of reading this paper, and, while I don't share your 
level of expertise, I modestly share your opinion that solving problems 
with dynamically generated classes unnecessarily complicates code. For 
example, the problem of the OP could be solved easily with a dictionary 
and two functions used to access the dictionary and then, if making a 
class was truely necessary, including these functions as members of the 
class. But the OP mentioned something about "properties" and all hell 
broke loose.

However, I think that what you are saying about metaclasses being 
brittle relates more to implementation than language. In theory, these 
should be equivalent:

(1)     class Bob(object): pass
(2)     Bob = type('Bob', (), {})

And indeed a cursory inspection of the resulting classes show that they 
are indistinguishable.

That they wouldn't be seems an implementation bug and perhaps that bug 
should be fixed rather than promoting the avoidance of (2) because it 
does not create classes that behave as number (1).

James


-- 
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/



More information about the Python-list mailing list