Order in metaclass

Carlos Ribeiro carribeiro at gmail.com
Wed Oct 13 17:19:40 CEST 2004


On Wed, 13 Oct 2004 10:42:28 -0400, Nicolas Fleury
<nid_oizo at yahoo.com_remove_the_> wrote:
> Carlos Ribeiro wrote:
> > Of course, we are now getting into corner cases that show how much are
> > we pushing class  statements in Python. The _sane_ way to make it all
> > work would be to have a hook to provide a user-defined dict to the
> > class locals() dir; aternatively, the natice dict() could provide a
> > ordered interface (but then it wouldn't be a simple hash mapping, a
> > more complex structure such as a tree would be needed). Both are far
> > from happening in Python 2.x, IMHO... and I really doubt if dicts will
> > ever be changed to accomodate ordering, even in Python 3.0. Too much
> > hassle for too little gain. A hook function seems to be more sensible.
> 
> I don't know.  I was thinking that maybe an ordereddict class would be
> useful, something like:
> 
> myDict = {'a': 4, 'b': 5 }  # normal dict
> myDict = o{'a': 4, 'b': 5 }  # ordered dict, keeping order ['a', 'b']
> 
> The dictionary could only be use in specific cases, like configuration
> stuff or temporarily when instantiating a metaclass...  I don't know,
> maybe I'm just crazy...

I don't think you're crazy. OTOH, defining 'ordering' is difficult. Is
it the alphabethical ordering, or is it the ordering of insertion?
Either way is arbitrary and it not going to satisfy everyone.

I am beginning to get a glimpse of the theorethical implications of
Python's metaclass design and implementation. It's an amazingly clever
solution (the __metaclass__ magic name, I mean); but I suspect that it
could be more powerful if there were a way to detect the metaclass
*before* the class statement is first processed; in this way, the
metaclass could provide a custom locals() dict, or implement other
types of hook for the class being created. But this is really hard
stuff, with implications beyond my current understanding, and I don't
feel prepared to argue at python-dev about it, really.


-- 
Carlos Ribeiro
Consultoria em Projetos
blog: http://rascunhosrotos.blogspot.com
blog: http://pythonnotes.blogspot.com
mail: carribeiro at gmail.com
mail: carribeiro at yahoo.com



More information about the Python-list mailing list