[Python-ideas] Null coalescing operators
mertz at gnosis.cx
Sun Sep 20 23:47:16 CEST 2015
Paul Moore's idea is WAAYY better than the ugly ? pseudo-operator.
`maybe()` reads just like a regular function (because it is), and we don't
need to go looking for Perl (nor Haskell) in some weird extra syntax that
will confuse beginners.
On Sun, Sep 20, 2015 at 4:05 AM, Paul Moore <p.f.moore at gmail.com> wrote:
> On 20 September 2015 at 08:31, Steven D'Aprano <steve at pearwood.info>
> > I'm not convinced that we should generalise this beyond the three
> > original examples of attribute access, item lookup and function call. I
> > think that applying ? to arbitrary operators is a case of "YAGNI". Or
> > perhaps, "You Shouldn't Need It".
> Does this need to be an operator? How about the following:
> class Maybe:
> def __getattr__(self, attr): return None
> def __getitem__(self, idx): return None
> def __call__(self, *args, **kw): return None
> def maybe(obj):
> return Maybe() if obj is None else obj
> attr = maybe(obj).spam
> elt = maybe(obj)[n]
> result = maybe(callback)(args)
> The Maybe class could be hidden, and the Maybe() object a singleton
> (making my poor naming a non-issue :-)) and if it's felt sufficiently
> useful, the maybe() function could be a builtin.
> Usage of the result of maybe() outside of the above 3 contexts should
> simply be "not supported" - don't worry about trying to stop people
> doing weird things, just make it clear that the intent is only to
> support the 3 given idiomatic usages.
> Python-ideas mailing list
> Python-ideas at python.org
> Code of Conduct: http://python.org/psf/codeofconduct/
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...
More information about the Python-ideas