[Python-ideas] A better (simpler) approach to PEP 505
David Mertz
mertz at gnosis.cx
Mon Jul 23 13:03:15 EDT 2018
On Mon, Jul 23, 2018 at 12:47 PM Antoine Pitrou <solipsis at pitrou.net> wrote:
> > favorite = cfg?.user?.profile?.food ?? "Spam"
> > favorite = NoneAware(cfg, "Spam").user.profile.food.unbox()
>
> You could use .__call__() instead of .unbox(). Also you can make
> unboxing unnecessary in most cases by having your class proxy most
> operations, like weakref.proxy does. The "wrapt" library may help with
> that: http://wrapt.readthedocs.io/en/latest/wrappers.html
I'm not sure I entirely understand how that class proxy could be made
transparent. Maybe you can make a toy implementation to show me? How can
we make this work?
favorite = NoneAware(cfg, "Spam").user.profile.food + "and more spam"
I just noticed in my scratch directory that I had implemented almost the
same thing a year ago when this discussion last came up. It's simple
enough that the code was almost the same back then. I had included
`.__getitem__()` in that version, but had not considered sentinels.
However, one thing I *did* implement was `.__call__()`, but that reminds me
of why your idea cannot work. I had it either call or fall back to more
boxing. The end point of the chained attributes (or dict lookups) can
perfectly well be a callable. E.g.
favorite = NoneAware(cfg, "Spam").user.profile.get_food()
Well, that's not going to work in my current toy code either since it would
need to be:
favorite = NoneAware(cfg, "Spam").user.profile.get_food.unbox()()
But I could implement special stuff for "if the tail is a callable, unbox
it automatically", I suppose.
However, it would be fundamentally ambiguous, I think, whether those final
parens should mean "unbox" or "call". Or I guess if calling *always* meant
"unbox" we could have:
favorite = NoneAware(cfg, "Spam").user.profile.get_food()()
That feels weird to me though.
--
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/20180723/50a05f8c/attachment.html>
More information about the Python-ideas
mailing list