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

Josiah Carlson jcarlson at uci.edu
Wed Feb 21 07:04:48 CET 2007


Larry Hastings <larry at hastings.org> wrote:
> 
> Steven Bethard wrote:
> > On 2/20/07, Larry Hastings <larry at hastings.org> wrote:
> >> I considered using __slots__, but that was gonna take too long.
> > Here's a simple implementation using __slots__:
> 
> Thanks for steering me to it.  However, your implementation and Mr. 
> Hettinger's original NamedTuple both requires you to establish a type at 
> the onset; with my prototype, you can create records ad-hoc as you can 
> with dicts and tuples.  I haven't found a lot of documentation on how to 
> use __slots__, but I'm betting it wouldn't mesh well with my breezy 
> ad-hoc records type.

If it helps, you can think of Steven's and Raymond's types as variations
of a C struct.  They are fixed at type definition time, but that's more
or less the point.

Also, you can find more than you ever wanted to know about __slots__ by
searching on google for 'python __slots__' (without quotes), but it
would work *just fine* with your ad-hoc method, though one thing to note
with your method - you can't guarantee the order of the attributes as
they are being displayed.  Your example would have the same issues as
dict does here:

    >>> dict(b=1, a=2)
    {'a': 2, 'b': 1}

Adding more attributes could further arbitrarily rearrange them.


 - Josiah



More information about the Python-Dev mailing list