[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 table 
> analyser cannot easily detect when names are re-used between the leftmost 
> comprehension iterable expression and the rest of the comprehension), named 
> expressions are disallowed entirely as part of comprehension iterable 
> expressions (the part after each "in", and before any subsequent "if" or 
> "for" keyword):"

Might the new PEG parser maybe help alleviate this restriction, so we could 
declare this a bug instead?

--
nosy: +Anselm Kiefner

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 of the comprehension), named 
expressions are disallowed entirely as part of comprehension iterable 
expressions (the part after each "in", and before any subsequent "if" or "for" 
keyword):"

The error message also makes it clear.

--
nosy: +eric.smith
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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.split('[-_. ]', n)[:-1]])}.{n.split('.')[-1]}")]

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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:
  File "C:\Users\1\Desktop\sn.py", line 4
[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: ')}\\")]

 ^
SyntaxError: assignment expression cannot be used in a comprehension iterable 
expression

Process finished with exit code 1

--

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 range(5)]
[(0, 1), (1, 2), (4, 5), (9, 10), (16, 17)]


What did you try, and what happened?

--
nosy: +steven.daprano

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com