Google
 
Webnews.only-4-geeks.com
Interesting places
news.only-4-geeks.com Forum Index » PythonGoto page 1, 2  Next

Class definition attribute order

 
Jump to:  
 
Andrew Lentvorski
PostPosted: Fri Aug 01, 2008 9:23 pm    Post subject: Class definition attribute order
       
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
 

 
Miles
PostPosted: Fri Aug 01, 2008 9:47 pm    Post subject: Re: Class definition attribute order
       
On Fri, Aug 1, 2008 at 7:23 PM, Andrew Lentvorski <bsder@allcaps.org> wrote:
Quote:
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.

-Miles
 

 
Ben Finney
PostPosted: Sat Aug 02, 2008 12:33 am    Post subject: Re: Class definition attribute order
       
Andrew Lentvorski <bsder@allcaps.org> writes:

Quote:
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
 

 
Benjamin
PostPosted: Sat Aug 02, 2008 2:47 am    Post subject: Re: Class definition attribute order
       
On Aug 1, 6:23pm, Andrew Lentvorski <bs...@allcaps.org> wrote:
Quote:
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.
Quote:

Thanks,
-a
 

 
Gabriel Genellina
PostPosted: Tue Aug 05, 2008 12:38 am    Post subject: Re: Class definition attribute order
       
En Fri, 01 Aug 2008 23:47:42 -0300, Benjamin <musiccomposition@gmail.com>
escribi:
Quote:
On Aug 1, 6:23pm, Andrew Lentvorski <bs...@allcaps.org> 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 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?

--
Gabriel Genellina
 

 
Michele Simionato
PostPosted: Tue Aug 05, 2008 3:05 am    Post subject: Re: Class definition attribute order
       
On Aug 5, 4:38am, "Gabriel Genellina":
Quote:

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 LINK
(I am working on an English translation these days,
but for the moment you can use Google Translator).

M. Simionato
 

 
Terry Reedy
PostPosted: Tue Aug 05, 2008 3:47 am    Post subject: Re: Class definition attribute order
       
Michele Simionato wrote:
Quote:
On Aug 5, 4:38 am, "Gabriel Genellina":
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 LINK
(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
 

 
Gabriel Genellina
PostPosted: Tue Aug 05, 2008 4:06 am    Post subject: Re: Class definition attribute order
       
En Tue, 05 Aug 2008 00:05:58 -0300, Michele Simionato
<michele.simionato@gmail.com> escribi:

Quote:
On Aug 5, 4:38am, "Gabriel Genellina":

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 LINK
(I am working on an English translation these days,
but for the moment you can use Google Translator).

Grazie, questo un modo per praticare il mio italiano!

--
Gabriel Genellina
 

 
Michele Simionato
PostPosted: Tue Aug 05, 2008 5:53 am    Post subject: Re: Class definition attribute order
       
On Aug 5, 7:47am, Terry Reedy <tjre...@udel.edu> wrote:
Quote:

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?
 

 
Terry Reedy
PostPosted: Tue Aug 05, 2008 4:14 pm    Post subject: Re: Class definition attribute order
       
Michele Simionato wrote:
Quote:

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.
 

Page 1 of 2 .:. Goto page 1, 2  Next

Google
 
Webnews.only-4-geeks.com

Windows Update | C++ | C | PHP | JavaScript | Photoshop | Programming | Windows 2000 | Python | Windows XP | Object | Flash | Flash - ActionScript | Paint Shop Pro | Excel | PowerPoint | Access | Word | Windows 98 | Internet Explorer 6.0 | CorelDraw12 | Java | XML | asm x86 | Linux Mandrake | Linux RedHat | Outlook |  | news from newsgroups |_ | s

Web Templates

Awesome Website Templates

pierścionek zaręczynowy Pozycjonowanie odzież dla dzieci gratka artykuły biurowe