The problem with doing that is that it's ambiguous. There's no way of
telling which attribute is allowed to coalesce.
I think one of the best arguments for a coalescing operator in Python is
that it allows you to be more explicit, without the hassle of nested try:
except AttributeError blocks. You lose that with something like
attrgetter('b.x.z', coalesce=True) -- it would behave identically,
regardless of whether b, x, or z were missing, which is (oftentimes) not
what you want.
Nick Badger
https://www.muterra.io
https://www.nickbadger.com
2016-10-27 15:28 GMT-07:00 Barry Warsaw <[email protected]>:
> 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
>
> _______________________________________________
> Python-ideas mailing list
> [email protected]
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/