Is this a legal / acceptable statement ?

bruno at modulix onurb at xiludom.gro
Fri May 5 16:46:53 CEST 2006


Philippe Martin wrote:
> Hi,
> 
> This code works, but is it "appropriate" ?

appropriate for what ?-)

> l_init = False
> # corrected typo, cf other post in this thread
> if True == l_init and 1234 == l_value:
>  print 'l_value is initialized'

Do this in production code, and have one of the first Python entry in
the Daily WTF !-)

What are you trying to do, exactly ? I've been scratching my head for at
least 5 minutes without finding any sensible reason to write such code.


> I know I can do this with a try but ...

???


<ot>
Slightly ot, but since you ask for idioms:

1/ Since binding (so-called 'assignment') is a statement (not an
expression), you can as well write your equality test the 'normal' way:

>>>  if l_init == True and l_value == 1234:
...      pass

the "litteral value == variable" idiom comes from languages where
assignement being an expression, one could 'typo' '=' for '==', usually
leading to unwanted result !-)

2/ also, testing for equality against True or False is a bad idea, since
there are some things that will eval to false in a boolean context
without actually being equal to False:
>>> for item in [[], (), {}, None, '']:
...     print item, " == False ?", item == False
...     if not item:
...             print "but still evals to False..."
...
[]  == False ? False
but still evals to False...
()  == False ? False
but still evals to False...
{}  == False ? False
but still evals to False...
None  == False ? False
but still evals to False...
  == False ? False
but still evals to False...
>>>

so :
>>> if l_init and l_value == 1234:
...     pass

This still doesn't make sense to me, but it's at least a bit more
readable !-)
</ot>

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list