[Python-Dev] A "record" type (was Re: Py2.6 ideas)

Josiah Carlson jcarlson at uci.edu
Wed Feb 21 09:28:12 CET 2007


Larry Hastings <larry at hastings.org> wrote:
> 
> Josiah Carlson wrote:
> > one thing to note with your method - you can't guarantee the order
> > of the attributes as they are being displayed.
> >   
> Actually, my record type *can*; see the hack using the __names__ field.  
> It won't preserve that order during iteration--but it's only a prototype 
> so far, and it could be fixed if there was interest.

Actually, it *can't*.  The ordering of the dict produced by the **kwargs
arguments is exactly same as a regular dictionary.

    >>> def foo(**k):
    ...     return k
    ...
    >>> foo(b=1, a=2)
    {'a': 2, 'b': 1}
    >>> foo(hello=1, world=2)
    {'world': 2, 'hello': 1}

After construction you can preserve the order it has, but you've already
lost the order provided in the call, so the order you get is arbitrarily
defined by implementation details of dictionaries and hash(str).


 - Josiah



More information about the Python-Dev mailing list