an ingrate newbie complains

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Wed Feb 4 13:57:19 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

If you iterate over a dict, you in fact iterate over the keys (which 
makes perfect sens ihmo, just think of the cases where you would iterate 
over a dict...). Now if you read the doc for the list() constructor, 
you'll see that it takes either a sequence or an iterable. Since a dict 
is not a sequence, but an iterable, with a defined behavior, I don't see 
anything puzzling nor disappointing here

> 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>
> 
> ...?

In C, an assignement *is* an expression, and this leads to

1/ a very common bug, ie :
char *buf = malloc(100);
if (buf = NULL) {
    /* some error code here */
}

2/ hardly readable code, ie:
char *buf;
if ((buf = malloc(100)) != NULL) {
    /* proceed with buf */
}

The BDFL designed Python to have a clear, readable, and as less 
error-prone syntax as possible.

> 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.
> 

if g(x) is a fairly complicated expression, you'd better stick with the 
common idiom anyway :

result = []
for x in thelist: #BTW, dont use 'list' as an identifier
     y = g(x)
     if y <= 0:
         result.append(f(x, y))

Now I must confess that I'd sometime like to have assignement as 
expressions too. But one particular feature of dictatorship is that the 
dictator dictates !-)

My 2 cents




More information about the Python-list mailing list