Philippe said:
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:
.... 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 :.... pass
This still doesn't make sense to me, but it's at least a bit more
readable !-)
</ot>