Re: [Python-Dev] The new and improved PEP 572, same great taste with 75% less complexity!

2018年4月26日 09:50:00 -0700

2018年04月24日 18:31 GMT+03:00 Chris Angelico <[email protected]>:
>
> Recommended use-cases
> =====================
>
> [...]
>
> # Capturing regular expression match objects
> # See, for instance, Lib/pydoc.py, which uses a multiline spelling
> # of this effect
> if match := re.search(pat, text):
> print("Found:", match.group(0))
>
>
Not sure, but if additional motivating examples are required, there is a
common pattern for dynamic attribute lookup (snippet from `copy.py`):
 reductor = dispatch_table.get(cls)
 if reductor:
 rv = reductor(x)
 else:
 reductor = getattr(x, "__reduce_ex__", None)
 if reductor:
 rv = reductor(4)
 else:
 reductor = getattr(x, "__reduce__", None)
 if reductor:
 rv = reductor()
 else:
 raise Error("un(shallow)copyable object of type %s" % cls)
which can with the current `binding expression` syntax simplified to:
 if reductor := dispatch_table.get(cls):
 rv = reductor(x)
 elif reductor := getattr(x, "__reduce_ex__", None):
 rv = reductor(4)
 elif reductor := getattr(x, "__reduce__", None):
 rv = reductor()
 else:
 raise Error("un(shallow)copyable object of type %s" % cls)
which becomes much clearer, at least in my opinion.
With kind regards,
-gdg
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to