an ingrate newbie complains

Peter Otten __peter__ at web.de
Wed Feb 4 13:46:28 EST 2004


Elaine Jackson wrote:

> I have some minor complaints about the Python language, and I'm interested
> to know how other people feel about them. Here they are:
> 
> 1) I find the following behavior puzzling and disappointing:
> 
>>>> X=[(1,1),(2,4),(3,9),(4,16),(5,25)]
>>>> Y=dict(X)
>>>> Z=list(Y)
>>>> Z==X
> False
>>>> Z==Y.keys()
> True

I too would have expected iter(d) to generate the same list as d.iteritems()
instead of d.iterkeys(). However, for me this is not a problem in practice
as I tend to be explicit, e. g

Z = Y.items()

which is also a good reminder that I'm converting a dictionary.

> 
> 2) How come you can't write...
> 
> if y = f(x) > 3:
>     <do a bunch of things with x and y>
> 
> ...as an equivalent of...
> 
> y = f(x)
> if y > 3:
>     <do a bunch of things with x and y>
> 
> ...?

Too much C programming considered harmful. What is the advantage of the
first version over the second? Suppose you knew Python and wanted to learn
C, wouldn't you complain on comp.lang.c about the unnecessary complexity?

> That kind of syntax would be especially welcome in list comprehensions:
> 
> [f(x,y) for x in list if y=g(x)<=0]
> 
> If g(x) is a fairly complicated expression, and y occurs several times in
> f(x,y), considerable clarity could be gained.

Is the above list comprehension that frequent? Then how about

[f(x, y) for x, y in [(x, g(x)) for x in lst] if cond(y)]

With the arrival of generator expressions, some of the overhead (the
intermediate list) is bound to go away. In the mean time, there's still
that good old for loop, which IMHO is still the most readible solution if
things get really complicated.

Peter




More information about the Python-list mailing list