开发者

Evaluating result of expression in if-statement in python [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to assign a variable in IF, and then return it. (Python)

In Python is there a better way to do this?:

myVal = getVal()

if not myVal:
  开发者_开发百科  continue

I'd like to do something like this:

if not (myVal = getVal()):
    continue

But that is not valid syntax. Are there any other ways to do this on one line like can be done in PHP and Perl?


Its not pythonic to have return value for assignment. In python, assignment means binding object to a name, not putting value in memory location.


No, you can't do this.

I'd say that's because it's too easy to confuse with if not (myVal == getVal()):.


No you can't do this.

However, almost all situations like this can be done more cleanly in python. However, in order to show you how to do that we need more context. In particular, how this loop you are continuing is constructed.


You could try this instead, (depending on what getVal can return):

if not getVal():
    continue
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜