Find class attributes creation order

M

Marcob

Let's see these simple classes:

class base_foo()
pass

class foo(base_foo):
a=1
b={}
c="zz"

I would like to find class foo attributes creation order.
Using __metaclass__ is not of help because special method __new__
receive attrs as a dictionary and so the order isn't preserved. Any
other idea?

Thanks for your time.

Ciao.
Marco.
 
C

Cédric Lucantis

Le Monday 23 June 2008 17:53:07 Marcob, vous avez écrit :
Let's see these simple classes:

class base_foo()
pass

class foo(base_foo):
a=1
b={}
c="zz"

I would like to find class foo attributes creation order.
Using __metaclass__ is not of help because special method __new__
receive attrs as a dictionary and so the order isn't preserved. Any
other idea?

I don't really understand what you want, but __new__ may accept any kind of
attributes, this is your choice. You can use named params or a vararg list
(which will preserve params order).

But in your example you're only using class attributes, so __new__ is not
involved and they are just created once for all in the order you write them:
a, b, c.
 
M

Marcob

Le Monday 23 June 2008 17:53:07 Marcob, vous avez écrit :





I don't really understand what you want, but __new__ may accept any kind of
attributes, this is your choice. You can use named params or a vararg list
(which will preserve params order).

Ok, I'll try to make myself clearer.
But in your example you're only using class attributes, so __new__ is not
involved and they are just created once for all in the order you write them:
a, b, c.

Yes, this is true but if another piece of code define class foo (and
consequently a, b, c) and I would like to find how many class
attributes are defined and their order, how can I do this?

It's easy to find how many attributes there are: using __dict__ or
dir() or whatever.

But to find their order isn't so easy.

Besides that I can do whatever I want with class base_foo but I can't
touch class foo.

Ciao.
Marco.
 
M

Michele Simionato

Let's see these simple classes:

    class base_foo()
         pass

    class foo(base_foo):
         a=1
         b={}
         c="zz"

I would like to find class foo attributes creation order.
Using __metaclass__ is not of help because special method __new__
receive attrs as a dictionary and so the order isn't preserved. Any
other idea?

You need to wait for Python 3.0 metaclasses:

http://stacktrace.it/articoli/2008/01/metaclassi-python-3000/ which
you should know already (ciao Marco! ;-)

Michele Simionato
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top