If/then style question
Jean-Michel Pichavant
jeanmichel at sequans.com
Fri Dec 17 09:41:09 EST 2010
Rob Richardson wrote:
> -----Original Message-----
> What about,
>
>
> def myMethod():
> for condition, exitCode in [
> (cond1, 'error1'),
> (cond2, 'very bad error'),
> ]:
> if not condition:
> break
> else:
> do_some_usefull_stuff() # executed only if the we never hit the
> break statement.
> exitCode = good1
>
> return exitCode
>
> ---- I reply -----
>
> This is interesting, but I don't understand it (which speaks volumes
> about the level of my understanding of Python).
>
> First, just to clarify, I don't think the indentation I saw was what was
> originally posted. The "else" must be indented to match the "if", and
> the two statements under "else" are in the else block.
No, the else is indented to the for loop.
for ... else is a python statement, the else block is executed only if
the loop did never break.
http://docs.python.org/reference/compound_stmts.html#for
> The return
> statement is indented at the same level as the for statement, so that it
> will be executed after the for loop exits. Correct?
>
> Now, the for loop will set condition to cond1 and exitCode to 'error1'.
> Then it checks the contents of the condition variable. But what does
> "not <variable_name>" by itself mean?
condition is a bool value.
if not condition is evaluated to True, if the condition is False.
condition = False
not condition => True
condition = ('Foo' == 'Foo')
not condition => False
[snip]
> RobR
>
>
My mail client could have messed up with the indentation.
Here is the code:
http://paste-it.net/public/t8a4acd/python/
JM
More information about the Python-list
mailing list