Class definition attribute order

  • Thread starter Andrew Lentvorski
  • Start date
A

Andrew Lentvorski

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?

Thanks,
-a
 
B

Benjamin

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 wait until Python 3.0 where you can do this sort of thing with
metaclasses.
 
G

Gabriel Genellina

You wait until Python 3.0 where you can do this sort of thing with
metaclasses.

So the namespace that the metaclass receives when the class is created,
will be some kind of ordered dictionary?
Metaclasses are available for a long time ago, but the definition order is
lost right at the start, when the class body is executed. Will this step
be improved in Python 3.0 then?
 
M

Michele Simionato

So the namespace that the metaclass receives when the class is created,  
will be some kind of ordered dictionary?
Metaclasses are available for a long time ago, but the definition order is  
lost right at the start, when the class body is executed. Will this step  
be improved in Python 3.0 then?

Yep. See http://stacktrace.it/articoli/2008/01/metaclassi-python-3000
(I am working on an English translation these days,
but for the moment you can use Google Translator).

M. Simionato
 
T

Terry Reedy

Michele said:
Yep. See http://stacktrace.it/articoli/2008/01/metaclassi-python-3000
(I am working on an English translation these days,
but for the moment you can use Google Translator).

Bfiefly, as I understood the discussion some months ago: In 2.x, the
class body is executed in a local namespace implemented as a normal dict
and *then* passed to the metaclass. In 3.0, the metaclass gets brief
control *before* execution so, among other possibilities, it can
substitute an (insertion) ordered dict for the local namespace. I will
leave the details to Michele's article and its eventual translation.

tjr
 
M

Michele Simionato

Bfiefly, as I understood the discussion some months ago: In 2.x, the
class body is executed in a local namespace implemented as a normal dict
and *then* passed to the metaclass.  In 3.0, the metaclass gets brief
control *before* execution so, among other possibilities, it can
substitute an (insertion) ordered dict for the local namespace.  I will
leave the details to Michele's article and its eventual translation.

tjr

BTW, since I do not really follow python-dev, do you know
if some consensus was reached on the issue of adding an ordered dict
implementation to the standard library?
 
T

Terry Reedy

Michele said:
BTW, since I do not really follow python-dev, do you know
if some consensus was reached on the issue of adding an ordered dict
implementation to the standard library?

I thought there was to be one added to collections, where default_dict
lives, but I do not remember seeing it. The discussion was on the
PY-3000 list. You could review the archives, or possibly the issues
list (bugs.python.org) or just ask there.
 
T

thebjorn

On Aug 5, 5:05 am, Michele Simionato



FWIW, I have just finished translating the first
part of the article and I have posted it on my
blog on Artima:

http://www.artima.com/weblogs/viewpost.jsp?thread=236234

Great feature and great article! I haven't used ABCs yet, so my
initial instinct would be to derive odict from dict (it would obviate
the conversions in the metaclass). Are you using ABCs just to play
with all the new toys at the same time? ;-)

-- bjorn
 
M

Michele Simionato

On Aug 10, 12:14 am, thebjorn
Great feature and great article!  I haven't used ABCs yet, so my
initial instinct would be to derive odict from dict (it would obviate
the conversions in the metaclass). Are you using ABCs just to play
with all the new toys at the same time? ;-)

If you look at the original Italian version I did derive
from dict originally; however since the ordered dict
implementation I show does not support __delitem__
(on purpose, to make it simple enough to fit in
a short paper) I thought it was more correct
to derive from Mapping, not from dict.

Michele Simionato
 
B

Benjamin

BTW, since I do not really follow python-dev, do you know
if some consensus was reached on the issue of adding an ordered dict
implementation to the standard library?

I believe it has been deferred to 2.7/3.1.
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top