The curious thing about PEP 505 as far as I can see is that it
introduces a new piece of syntax -- and for many people (to judge from
the reactions here) a controversial piece of syntax -- to solve what
seems to be a rather specific problem.  The use-case that seems most
discussed is unpacking information from nested JSON.

One of the things that makes me really twitch about the examples like

food = ham?.eggs?.spam?.chips

is that I don't usually deal with deeply nested structures like that
where it wouldn't matter to me which level of the nest returned a
value.

But let's leave that aside, because it seems to be an issue people
face. For the specific case mentioned, what would be wrong with a
utility function that looked something like:

# Warning - typed on gmail before I've had my morning coffee and so not
# at all tested, but I hope you can see what I mean.

def nested_conditional_find(search_this, *args):
     this = search_this
     for term in args:
          new_this = getattr(search_this, term)
          if new_this is None:
               return this
          else:
               this = new_this

then you could use this function as:

food = nested_conditional_find(ham, 'eggs', 'spam', 'chips')

One could even imagine adding extra tests along the way to a function
like that, raising attribute errors if the wrong kind of data was
encountered etc.

What problem does PEP 505 add that can't be solved very nearly as
elegantly by a utility function?  PEP 505 doesn't seem to be about
speed, only convenience.

For that matter, for specific use cases, why couldn't objects be
created where the attribute access functioned in just this kind of
way?  (not example code I'm willing to attempt before coffee ;-) )

Just a thought.

I've carefully avoided words like 'pythonic', 'unpythonic',
'explicit', 'implicit', 'compact', 'bug-magnet' or 'perl'.

Best wishes,

N

P.S. As an aside, in examples like food = spam?.eggs?.bacon -- if
bacon is None then food is None, even if there were in fact eggs and
spam.  That thought tells me it must be breakfast time.
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to