Booleans, integer division, backwards compatibility; where is Python going?

Alex Martelli aleax at aleax.it
Wed Apr 10 05:53:53 EDT 2002


Paul Boddie wrote:
        ...
>> Nothing is more natural to a beginner than "if x in somedict:" and
>> "for x in thefile:" -- having had the opportunity to teach some
>> beginners with 2.2, I've seen them slurp these up instantly (just
>> try to contrast with "if somedict.has_key(x):" and
>> "for x in thefile.xreadlines():" ...!).
> 
> Well, both examples you gave seemed to be controversial when up for
> discussion in the past, but if you say they help people, then I'll
> take your word for it.

I've noticed they immensely help Python beginners that come to them
without preconcepts.  Such beginners are surprised to learn that
these are extremely recent additions to Python, so well they fit in.

Human beings can grow used to anything, and sometimes get set in their
ways, so I can't judge how well people already expert of Python are
taking them.  Still, "for x in thedict:" is faster than "for x in 
thedict.keys():", so it's not only syntax sugar.


>> And the new features are the right way to implement such obvious progress
>> -- iterators, for example, let you loop on (e.g.) dictionaries or files,
>> without requiring such types to define unnatural (or unfeasible)
>> "indexing" then go out of their way to stop misuse.
> 
> Iterators are indeed nice to use and a very welcome feature.

They're great progress over the previously murky "iteration protocol".


>> > advanced/experienced Python developer.
>> 
>> You MUST be joking -- inheriting from builtin types is SUCH a win,
>> and so obviously so, for advanced and experienced developers!
> 
> Perhaps I'm not writing the kinds of applications where this is the
> case, then.

Or maybe not being used to them you just don't think of cases where
they'd be handy (if you need to keep compatibility with 2.1, then
of course you'd better NOT start thinking of such cases:-).  I.e.,
Sapir-Whorf hypothesis somewhere between its weakest and strongest
forms (though I still prefer the way Wittgenstein independently
put it earlier -- "The limits of my language are the limits of
my world").  Mappings and sequences are such useful concepts that
I doubt you don't have such objects in your applications -- and
it's such a frequent occurrence nowadays for the best implementation
strategy for mappings or sequences to involve subclassing builtins,
that, while possible, it would be peculiar if none of your cases
fell into this category (always IF you don't need to care about
Python 2.1 and earlier, of course!).


>> Generators need a "from __future__ import", so I applaud the tutorial's
>> choice not to burden beginners with them YET.
> 
> It would be nice for some coverage of that particular feature, though.

Yes, just not in the tutorial (at least not until they're in the
"current" language rather than needing import from the future).

> I can't say that I've ever written one myself, although they do seem
> useful for certain kinds of applications.

Just basically all of those where you have loops; if your applications
have no loops (including ones obtained by recursion), then generators
are not of much interest:-).

Seriously: generators are a way to factor most looping control
structures' logic into specific reusable objects.  You can always
do that with iterators built in other ways (just classes with
a 'next' method), but generators are simpler and more concise
in a reasonably wide number of cases, basically because they
directly map the reasonably frequent case where it's natural to
keep part of a control structure's state in terms of "_where_
in this source code am I executing now".

Iterators (and generators in particular) are also a nice canonic
way to model many kinds of "lazy evaluation" -- I find that
part especially exciting -- but that's a rather different way
of looking at things.


>> > other things to do during working hours) it becomes increasingly hard
>> > to keep track of what's great in the more recent Python releases.
>> 
>> I think AMK's "what's new" articles are great for that purpose.
> 
> I must find the time to review them at my leisure, then.

I think you'll be glad you do -- they're concise and to the point.

AMK's mission is not to tell you WHY you can be interested in
major stuff such as subclassing builtins, iterators, generators,
though -- just to ensure you DO know easily what IS new.


Alex




More information about the Python-list mailing list