Class definition attribute order

Miles semanticist at
Sat Aug 2 01:47:51 CEST 2008

On Fri, Aug 1, 2008 at 7:23 PM, Andrew Lentvorski <bsder at> wrote:
> How do I determine the order of definition of class attributes?
> For example, if I have a class
> class Test(object):
>    y = 11
>    x = 22
> How do I tell that y was defined before x?

You can't.  The order that the locals are bound is not something
that's preserved.

There are frameworks that simulate this, but they actually just store
the order that the attributes are initialized:

class Test(FrameworkClass):
    y = FrameworkObject(11)
    x = FrameworkObject(22)

where FrameworkObject is assigned a number from an incrementing
counter on initialization, and FrameworkClass has a metaclass that
sorts those objects when the class is created.


More information about the Python-list mailing list