Walrus operator support
See original GitHub issueI wanted to try out the new assignment expressions aka walrus operator in Python 3.8.
It all worked fine in plain Python 3.8:
$ python
Python 3.8.0a1 (default, Feb 19 2019, 11:26:51)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> sample_data = [
... {"userId": 1, "id": 1, "title": "delectus aut autem", "completed": False},
... {"userId": 1, "id": 2, "title": "quis ut nam facilis", "completed": False},
... {"userId": 1, "id": 3, "title": "fugiat veniam minus", "completed": False},
... {"userId": 1, "id": 4, "title": "et porro tempora", "completed": True},
... {"userId": 1, "id": 4, "title": None, "completed": True},
... ]
>>>
>>> print("With Python 3.8 Walrus Operator:")
With Python 3.8 Walrus Operator:
>>> for entry in sample_data:
... if title := entry.get("title"):
... print(f'Found title: "{title}"')
...
Found title: "delectus aut autem"
Found title: "quis ut nam facilis"
Found title: "fugiat veniam minus"
Found title: "et porro tempora"
But failed miserably in IPython:
$ ipython
Python 3.8.0a1 (default, Feb 19 2019, 11:26:51)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.4.0.dev -- An enhanced Interactive Python. Type '?' for help.
In [1]: sample_data = [
...: {"userId": 1, "id": 1, "title": "delectus aut autem", "completed": False},
...: {"userId": 1, "id": 2, "title": "quis ut nam facilis", "completed": False},
...: {"userId": 1, "id": 3, "title": "fugiat veniam minus", "completed": False},
...: {"userId": 1, "id": 4, "title": "et porro tempora", "completed": True},
...: {"userId": 1, "id": 4, "title": None, "completed": True},
...: ]
...:
...: print("With Python 3.8 Walrus Operator:")
...: for entry in sample_data:
...: if title := entry.get("title"):
...: print(f'Found title: "{title}"')
...:
With Python 3.8 Walrus Operator:
---------------------------------------------------------------------------
SystemError Traceback (most recent call last)
/usr/local/lib/python3.8/codeop.py in __call__(self, source, filename, symbol)
131
132 def __call__(self, source, filename, symbol):
--> 133 codeob = compile(source, filename, symbol, self.flags, 1)
134 for feature in _features:
135 if codeob.co_flags & feature.compiler_flag:
SystemError: unexpected expression
I was surpised to see that happen, as IPython 7.3.0+ should have Python 3.8 support.
$ python -c "import IPython; print(IPython.sys_info())"
{'commit_hash': '19b47fa',
'commit_source': 'repository',
'default_encoding': 'utf-8',
'ipython_path': '/home/piotr/Personal/ipython/IPython',
'ipython_version': '7.4.0.dev',
'os_name': 'posix',
'platform': 'Linux-4.15.0-45-generic-x86_64-with-glibc2.17',
'sys_executable': '/home/piotr/.virtualenvs/ipython-KlOhEFsK/bin/python',
'sys_platform': 'linux',
'sys_version': '3.8.0a1 (default, Feb 19 2019, 11:26:51) \n'
'[GCC 5.4.0 20160609]'}
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
The Walrus Operator: Python 3.8 Assignment Expressions
In this tutorial, you'll learn about assignment expressions and the walrus operator. The biggest change in Python 3.8 was the inclusion of the...
Read more >What's New In Python 3.8 — Python 3.11.1 documentation
It is affectionately known as “the walrus operator” due to its ... To support both 3.8 and older, try pkg-config python-X.Y-embed --libs first...
Read more >The Walrus Operator in Python
The walrus operator, introduced in Python 3.8, offers a way to accomplish two tasks at once: assigning a value to a variable, and...
Read more >Python's walrus operator
Python's "walrus operator" is used for assignment expressions. Assignment expressions are a way of embedding an assignment statement inside another line of ...
Read more >You Should Be Using Python's Walrus Operator - Here's Why
The assignment operator - or walrus operator as we all know it - is a ... then it can help you make your...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Reported upstream: https://bugs.python.org/issue36332
The upstream issue was fixed and the fix is available with Python 3.8 alpha 3. Verified the same on master. I think this can be closed.