[issue38556] Walrus operator in list comprehensions [Python 3.8.0]

2020-07-11 Thread Anselm Kiefner
Anselm Kiefner added the comment: I just stumbled over this same restriction and when I googled for "SyntaxError: cannot assign to named expression", 0 actual results showed - an absolute unicorn for a Python error. > "Due to design constraints in the reference implementation (the symbol tab

[issue38556] Walrus operator in list comprehensions [Python 3.8.0]

2019-10-22 Thread Eric V. Smith
Eric V. Smith added the comment: This is explicitly mentioned in PEP 572 as being disallowed: "Due to design constraints in the reference implementation (the symbol table analyser cannot easily detect when names are re-used between the leftmost comprehension iterable expression and the rest

[issue38556] Walrus operator in list comprehensions [Python 3.8.0]

2019-10-22 Thread EGN
EGN added the comment: Even the simple code like this doesn't work: [print(p) for n in (p := ['a', 'b', 'c'])] -- ___ Python tracker ___ _

[issue38556] Walrus operator in list comprehensions [Python 3.8.0]

2019-10-22 Thread EGN
EGN added the comment: But if I'm taking main for loop out of the square brackets everything works fine. from os import getcwd, listdir, rename import re for n in listdir(p := f"{getcwd()}\\{input('Folder: ')}\\"): [rename(f'{p}{n}', f"{p}{''.join([w[:3] if len(w) > 3 else w for w in re

[issue38556] Walrus operator in list comprehensions [Python 3.8.0]

2019-10-22 Thread EGN
EGN added the comment: from os import getcwd, listdir, rename import re [rename(f'{p}{n}', f"{p}{''.join([w[:3] if len(w) > 3 else w for w in re.split('[-_. ]', n)[:-1]])}.{n.split('.')[-1]}") for n in listdir(p := f"{getcwd()}\\{input('Folder: ')}\\")] When I run this code, I'm getting:

[issue38556] Walrus operator in list comprehensions [Python 3.8.0]

2019-10-22 Thread Steven D'Aprano
Change by Steven D'Aprano : -- components: +Interpreter Core type: enhancement -> behavior ___ Python tracker ___ ___ Python-bugs-li

[issue38556] Walrus operator in list comprehensions [Python 3.8.0]

2019-10-22 Thread Steven D'Aprano
Steven D'Aprano added the comment: We're not mind-readers, how do you expect us to know what you tried if you don't tell us? The walrus operator works for me: >>> [spam for c in "hello world" if (spam:=c.upper()) in 'AEIOU'] ['E', 'O', 'O'] >>> [(spam:=x**2, spam+1) for x in ran

[issue38556] Walrus operator in list comprehensions [Python 3.8.0]

2019-10-22 Thread Karthikeyan Singaravelan
Karthikeyan Singaravelan added the comment: Can you please post a short snippet of what you are trying to do and the issue you are facing like traceback if any? -- nosy: +xtreak ___ Python tracker _

[issue38556] Walrus operator in list comprehensions [Python 3.8.0]

2019-10-22 Thread EGN
Change by EGN : -- title: Walrus operator in Python 3.8.0 in list comprehensions -> Walrus operator in list comprehensions [Python 3.8.0] ___ Python tracker ___ __