map float string '0.0' puzzle
Brian Quinlan
brian at sweetapp.com
Tue Jan 27 18:10:55 EST 2004
> >>> row = ('0.0', '1.0', None)
> >>> map(lambda setting: setting and float(setting) or None, row)
> Specifically, why is the return value of the first map operation:
> [None, 1.0, None]
>
> I was expecting:
> [0.0, 1.0, None]
Can you explain why you were expecting that? The expression is:
[('0.0' and 0.0) or None, ('1.0' and 1.0) or None, (None and ?) or None)]
^ False ^ True ^ False
You can look at and/or like this (except that function calls don't do
short-circuit evaluation):
def and(*args):
for a in args:
if not a:
return a
return args[-1]
def or(*args):
for a in args:
if a:
return a
return args[-1]
So you have:
or(and(settings, float(setting)), None)
Cheers,
Brian
More information about the Python-list
mailing list