Class definition attribute order

Ben Finney bignose+hates-spam at benfinney.id.au
Fri Aug 1 22:33:06 EDT 2008


Andrew Lentvorski <bsder at allcaps.org> writes:

> 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?

Like any namespace, attributes of an object are implemented as a
dictionary. Dictionaries have no guaranteed order to their items.

If you want to preserve order, do so with a type that preserves such
order:

class Test(object):
    ordered_things = [
        ('y', 11),
        ('x', 22),
        ]

If that doesn't meet your needs, perhaps you could describe what
problem you're trying to solve and we can address it better.

-- 
 \           “We spend the first twelve months of our children's lives |
  `\          teaching them to walk and talk and the next twelve years |
_o__)           telling them to sit down and shut up.” —Phyllis Diller |
Ben Finney



More information about the Python-list mailing list