On Oct 27, 2016, at 06:27 PM, Joonas Liik wrote:

>perhaps just having a utility function can get us some of the way there..
>
>#may error
>r = a.b.x.z
>
># will default to None
>r = a?.b?.x?.z
>r = get_null_aware(a, "b.x.z") # long but no new syntax, can be
>implemented today.

You could probably do this by extending operator.attrgetter() to take an
optional 'coalesce' keyword.  It wouldn't be super pretty, but it has the
advantage of no magical new syntax.  E.g. your example would be:

from operator import attrgetter
r = attrgetter('b.x.z', coalesce=True)

That might be good enough for honestly how rare I think this use case is.
(Similarly with itemgetter().)

Cheers,
-Barry

Attachment: pgpzYdAm4WHDd.pgp
Description: OpenPGP digital signature

_______________________________________________
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