In the examples below x is assigned using the walrus operator and is then printed.
mystring = "hello, world"
#if 1
if x := mystring == "hello, world":
print(x)
#if 2
if x := (mystring == "hello, world"):
print(x)
#if 3
if (x := mystring) == "hello, world":
print(x)
#if 4
if "hello, world" in (x := mystring):
print(x)
The Output:
True
True
hello, world
hello, world
I believe that I understand how each of these if statements works...
verbosely:
#if 1and#if 2are the same thing where the string comparison==returnsTrue, and that value is reassigned tox.#if 3assignsmystringasxbefore checking the truth of this equal to"hello, world".#if 4assignsmystringasxthen checks if"hello, world"is in this string.
So
What about this final #if 5 is ambiguous to Python such that it gives a Syntax Error?
#if 5
if "hello, world" in x := mystring:
print(x)
SyntaxError: cannot use assignment expressions with comparison
Is this related to PEP 572 Exceptional Cases:
Unparenthesized assignment expressions are prohibited at the top level of an expression statement.
there is no syntactic position where both are valid.
In reality I am looping through the pathlib Path.iterdir() generator object that yields strings with .stem. This does not change the result:
for file in folder.iterdir():
x := file.stem == "mystring":
print(x)
inand other comparison operators have a higher precedence than:=. So when you writeit means:
which is invalid as it tries to assign to
"hello, world" in x. To get it you work you need to use parentheses: