Order in metaclass

Nicolas Fleury nid_oizo at yahoo.com_remove_the_
Wed Oct 13 16:39:22 CEST 2004


Peter Otten wrote:
> If you want to record the order of these definitions, you need to pass a
> custom dictionary that keeps track of assignments in the class generation
> code (basically a normal python function comprising the class suite).
> Unfortunately that dictionary - which you see later as the classdict
> parameter of the metaclass __new__() method - is always a dict created in C,
> as was recently discussed on c.l.py (sorry, but all keywords I remember are
> 'metaclass' and 'martelli' - not very selective :-). Below is my (clumsy)
> attempt for a workaround:
> 
> import itertools
> 
> class OrderedValue(object):
>     newIndex = itertools.count(1).next
>     def __init__(self, value):
>         self.value = value
>         self.index = self.newIndex()

That's the solution I finally did after my post (I think I found the 
thread you were referring to).  In my case, having to create instances 
has not been so bad, as I can use them to put other information:

class SomeClass(BinType):
     x = Member(Int32)
     y = Member(Int16)

class SomeOtherClass(BinType):
     a = ArrayMember(Int16, size=256)
     b = Member(SomeClass)
     c = Member(Int32)

This way, I can generate abstract documentation of my binary formats 
without instantiating my types and can instantiate them to dump binary data.

Thx and Regards,
Nicolas



More information about the Python-list mailing list