[Python-ideas] Ordered storage of keyword arguments

Antoine Pitrou solipsis at pitrou.net
Fri Oct 29 10:49:31 CEST 2010


On Fri, 29 Oct 2010 10:30:26 +0200
"M.-A. Lemburg" <mal at egenix.com> wrote:
> Greg Ewing wrote:
> > M.-A. Lemburg wrote:
> >> Python has always tried
> >> to make the most common use case simple, so asking programmers to
> >> use a meta-class to be able to access the order of definitions
> >> in a class definition isn't exactly what the normal Python
> >> programmer would expect.
> > 
> > But needing to know the order of definitions in a class
> > is a very uncommon thing to want to do in the first
> > place.
> 
> I've already pointed to a couple of existing use cases where the
> authors had to play all sorts of tricks to access the order of
> such definitions.
> 
> Since Python programs are executed sequentially (within the resp.
> scope) in the order given in the source file, it is quite natural
> to expect this order to be accessible somehow.
> 
> If it were easier to access this order, a lot of the extra magic
> needed to map fixed order records to Python classes could go
> away.

Interestingly, this order is already accessible on the code object used
to build the class namespace:

>>> def f():
...   class C:
...     x = 5
...     def y(): pass
...     z = 6
... 
>>> code = f.__code__.co_consts[1]
>>> code.co_names
('__name__', '__module__', 'x', 'y', 'z')

Regards

Antoine.





More information about the Python-ideas mailing list