<div dir="auto"><div>The default could be at the end with an argument to unboxing :<div dir="auto"><br></div><div dir="auto"><span style="font-family:sans-serif">favorite = NoneAware(cfg).user.profile.food.unbo</span><span style="font-family:sans-serif">x("Spam")</span><br></div><br><br><div class="gmail_quote"><div dir="ltr">Le lun. 23 juil. 2018 à 17:26, Paul Moore <<a href="mailto:p.f.moore@gmail.com">p.f.moore@gmail.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">On 23 July 2018 at 16:12, David Mertz <<a href="mailto:mertz@gnosis.cx" target="_blank" rel="noreferrer">mertz@gnosis.cx</a>> wrote:<br>
> The need addressed by PEP 505 is real; it's also MUCH more niche and<br>
> uncommon than something that would merit new syntax.  Moreover, the actual<br>
> legitimate purpose served by the PEP 505 syntax is easily served by existing<br>
> Python simply by using a wrapper class.<br>
><br>
> Here is a way of solving the "deep attribute access to messy data" problem<br>
> that is:<br>
><br>
> (1) Much more explicit<br>
> (2) Requires no change in syntax<br>
> (3) Will not be a bug magnet<br>
> (4) Inasmuch as there are semantic traps, they are announced by the use of a<br>
> class whose documentation would be pointed to for readers<br>
<br>
[...]<br>
<br>
> I haven't implemented the equivalent dictionary lookups in the below.  That<br>
> would be straightforward, and I'm sure my 5 minute throwaway code could be<br>
> improved in other ways also.  But something better than this in the standard<br>
> library would address ALL the actual needs described in PEP 505.  Even the<br>
> pattern Steve Dower is especially fond of like:<br>
><br>
> favorite = cfg?.user?.profile?.food ?? "Spam"<br>
><br>
><br>
> (i.e. a configuration may be incomplete at any level, if levels are missing<br>
> default favorite food is Spam).  We could simply spell that:<br>
><br>
> favorite = NoneAware(cfg, "Spam").user.profile.food.unbox()<br>
<br>
Thank you. That's the sort of "what would this look like if<br>
implemented as a library rather than language syntax" solution that I<br>
had in mind with my earlier post.<br>
<br>
I would be very interested to hear discussion of the pros and cons of<br>
adding new syntax to the language as per PEP 505 in comparison to a<br>
solution like this (ultimately more fleshed out and "production<br>
quality") rather than comparisons PEP 505 to raw "roll your own"<br>
Python code.<br>
<br>
For me:<br>
<br>
* Library solution works with all versions of Python<br>
* The need for unbox is a little ugly, but arguably less so than ?.<br>
(conceded that's a subjective view)<br>
* Mixing ?. and . is terser than unboxing and reboxing - but are there<br>
any real examples where that's needed?<br>
* Having the default at the beginning rather than at the end doesn't<br>
follow natural reading order (but again that's pretty subjective)<br>
<br>
There's little here that isn't subjective, so I expect a lot of heated<br>
debate that ultimately convinces no-one. But the "you can use this<br>
solution now" aspect of the library solution seems pretty compelling<br>
to me - at least in terms of "let's publish the library solution and<br>
then based on experience with it, review PEP 505".<br>
<br>
Paul<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank" rel="noreferrer">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div></div></div>