Comment on PEP-0322: Reverse Iteration Methods

John Roth newsgroups at jhrothjr.com
Sat Sep 27 12:47:14 EDT 2003


"David Abrahams" <dave at boost-consulting.com> wrote in message
news:ullsa5hui.fsf at boost-consulting.com...
> Alex Martelli <aleax at aleax.it> writes:
> >
> > So, given an arbitrary struct, with fields (generally) of different
> > types, you can iterate field by field?
>
> Ah, no.  A tuple in C++ is a different thing, and much more like a
> Python tuple: http://www.boost.org/libs/tuple
>
> > That's basically what the "heterogeneous tuple" is supposed to be
> > equivalent to, in the "party line" I was relating
>
> Well, that's just nutty.  We can make a much better analogy to a
> struct in Python with the usual class hacks.  But even then, we can
> arrange to iterate the attributes.
>
> >  (although the names to go with the fields are only present in a FEW
> > such tuples, such as those returned by modules time and stat,
>
> Whaa??  Are they subclassing tuple?  Nope, time.struct_time is not
> even a tuple.  It's just the struct hack with a tuple-like repr()
>
> >>> time.localtime()
> (2003, 9, 27, 12, 10, 43, 5, 270, 1)
> >>> type(time.localtime())
> <type 'time.struct_time'>
> >>> time.struct_time.__bases__
> (<type 'object'>,)

Not exactly. It's an object that can be subscripted like a
sequence, so it looks like a tuple for the most common
use cases. I haven't looked at the code, so I don't
know how far they went in putting in the remainder of
tuple behavior, though.

The subscripting and slicing is the key point here, though.
That's what makes it backward compatible. It's not an
extended tuple, and I doubt if it has most of the tuple
methods.

Frankly, I prefer to call it a data object, rather than
a "struct hack." Calling it a data object means that
it might grow other useful behaviors in the future. I'm
not sure about time, but I could appreciate methods
in stat that apply the access and modify dates to another
file in one operation.

>
> > as of now; more general tuples still haven't grown the ability to
> > access fields by name, in Python).

And they most likely won't. It's easy enough to create
a data object that implements the basic part of the sequence
protocol.

John Roth

>
> -- 
> Dave Abrahams
> Boost Consulting
> www.boost-consulting.com






More information about the Python-list mailing list