[Python-ideas] PEP 505: None-aware operators
David Mertz
mertz at gnosis.cx
Thu Jul 26 00:16:55 EDT 2018
On Thu, Jul 26, 2018 at 12:00 AM Nicholas Chammas <
nicholas.chammas at gmail.com> wrote:
> Forgive me for being slow. I'm missing what's different in semantics
> between the translation above and Chris's translation below:
>
You are VERY close now. You have more SPAM, so yours is better:
In [1]: class Spam:
...: @property
...: def eggs(self):
...: print("SPAM SPAM SPAM")
...: return "eggs"
...:
In [2]: spam = Spam()
In [3]: _tmp = spam
In [4]: if _tmp is not None:
...: _tmp = _tmp.eggs
...: if _tmp is not None:
...: _tmp = _tmp.bacon
...: food = _tmp
...: del _tmp
...: food
...:
SPAM SPAM SPAM
---------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-4-9a2f963239f8> in <module>()
2 _tmp = _tmp.eggs
3 if _tmp is not None:
----> 4 _tmp = _tmp.bacon
5 food = _tmp
6 del _tmp
AttributeError: 'str' object has no attribute 'bacon'
In [5]: if spam is not None and spam.eggs is not None:
...: food = spam.eggs.bacon
...:
SPAM SPAM SPAM
SPAM SPAM SPAM
---------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-5-a24834cdb92b> in <module>()
1 if spam is not None and spam.eggs is not None:
----> 2 food = spam.eggs.bacon
3
AttributeError: 'str' object has no attribute 'bacon'
--
Keeping medicines from the bloodstreams of the sick; food
from the bellies of the hungry; books from the hands of the
uneducated; technology from the underdeveloped; and putting
advocates of freedom in prisons. Intellectual property is
to the 21st century what the slave trade was to the 16th.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180726/a221ebd4/attachment.html>
More information about the Python-ideas
mailing list