Python If Expression Assignment Of Deed

In Python, assignment is a statement, not an expression, and can therefore not be used inside an arbitrary expression. This means that common C idioms like:


cannot be used as is in Python. The first example is better written using an iterator:

for line in file: do something with line

but the second example must be written as:

match = search(target) if match: do something with match

and loops where an iterator cannot be used have to be written as:

while True: line = tryit.unas.czne() ifnot line: break do something with line

(this is often seen in older Python code, since earlier Python versions didn&#;t support iteration over file objects).

The reason for not allowing assignment in Python expressions is a common, hard-to-find bug in those other languages, caused by this construct:

The error is a simple typo: x = 0, which assigns 0 to the variable x, was written while the comparison x == 0 is certainly what was intended.

Many alternatives have been proposed. Most are hacks that save some typing but use arbitrary or cryptic syntax or keywords, and fail the simple criterion for language change proposals: it should intuitively suggest the proper meaning to a human reader who has not yet been introduced to the construct.

An interesting phenomenon is that most experienced Python programmers recognize the idiom and don&#;t seem to be missing the assignment in expression construct much; it&#;s only newcomers who express a strong desire to add this to the language.

There&#;s an alternative way of spelling this that seems attractive, but is generally less robust than the solution.

line = tryit.unas.czne() while line: do something with line line = tryit.unas.czne()

This pattern is known as , and works well for simple cases, but if you change your mind about exactly how you get the next line (e.g. you want to change it into tryit.unas.czne) you have to remember to change two places in your program &#; the second occurrence is hidden at the bottom of the loop.

In general, the best approach for looping is to use iterators, and loop through the objects using the for statement. If you need to repeatedly call a function or method to get the next value, you can use the iter wrapper function to turn the callable into an iterator:

for line in iter(tryit.unas.czne, ""): do something with line

Here, the loop will terminate when it reaches the end of the file (which causes readline to return an empty string).

CATEGORY: general

CATEGORY: design


1 PEP Conditional Expressions

For a long time, people have been requesting a way to write conditional expressions, which are expressions that return value A or value B depending on whether a Boolean value is true or false. A conditional expression lets you write a single assignment statement that has the same effect as the following:

if condition: x = true_value else: x = false_value

There have been endless tedious discussions of syntax on both python-dev and A vote was even held that found the majority of voters wanted conditional expressions in some form, but there was no syntax that was preferred by a clear majority. Candidates included C's , , and 16 other variations.

Guido van Rossum eventually chose a surprising syntax:

x = true_value if condition else false_value

Evaluation is still lazy as in existing Boolean expressions, so the order of evaluation jumps around a bit. The expression in the middle is evaluated first, and the expression is evaluated only if the condition was true. Similarly, the expression is only evaluated when the condition is false.

This syntax may seem strange and backwards; why does the condition go in the middle of the expression, and not in the front as in C's ? The decision was checked by applying the new syntax to the modules in the standard library and seeing how the resulting code read. In many cases where a conditional expression is used, one value seems to be the 'common case' and one value is an 'exceptional case', used only on rarer occasions when the condition isn't met. The conditional syntax makes this pattern a bit more obvious:

contents = ((doc + '\n') if doc else '')

I read the above statement as meaning ``here is usually assigned a value of ; sometimes is empty, in which special case an empty string is returned.'' I doubt I will use conditional expressions very often where there isn't a clear common and uncommon case.

There was some discussion of whether the language should require surrounding conditional expressions with parentheses. The decision was made to not require parentheses in the Python language's grammar, but as a matter of style I think you should always use them. Consider these two statements:

# First version -- no parens level = 1 if logging else 0 # Second version -- with parens level = (1 if logging else 0)

In the first version, I think a reader's eye might group the statement into 'level = 1', 'if logging', 'else 0', and think that the condition decides whether the assignment to is performed. The second version reads better, in my opinion, because it makes it clear that the assignment is always performed and the choice is being made between two values.

Another reason for including the brackets: a few odd combinations of list comprehensions and lambdas could look like incorrect conditional expressions. See PEP for some examples. If you put parentheses around your conditional expressions, you won't run into this case.

See About this document for information on suggesting changes.

0 thoughts on “Python If Expression Assignment Of Deed

Leave a Reply

Your email address will not be published. Required fields are marked *