[Python-ideas] PEP 505: None-aware operators
Rhodri James
rhodri at kynesim.co.uk
Mon Jul 30 10:10:28 EDT 2018
On 29/07/18 16:12, Abe Dillon wrote:
> spam?.eggs.cheese.aardvark # why would you ever do this?
If you knew that if you really have something in "spam", your program
guarantees it will have an "eggs" attribute with a "cheese" attribute,
etc, you just don't know if "spam" is not None. It's the same insider
knowledge you would use if you wrote it out as
spam.eggs.cheese.aardvark if spam is not None else None
The same sort of knowledge of your program structure could lead to you
to use "?." instead of "." in any place or places in the chain. If your
program gives you strong enough guarantees, it is the sort of thing you
can use to reduce the code's workload.
By way of example, I was working on some C code last night that was
expanding an internal buffer, possibly from non-existence. It did lot
of taking the difference between various pointers to determine how big
the new buffer needed to be, how much to copy from the old buffer, etc.
It looked rather hairy until you realised that the code gave a strong
guarantee that either all the pointers were meaningful or they were all
NULL, so the pointer differences always made sense. I rewrote that code
so that it didn't take differences of NULL pointers since that was what
the bug specified, but honestly it looks lumpier and less clear now. A
big comment explaining what was going on would probably be better.
--
Rhodri James *-* Kynesim Ltd
More information about the Python-ideas
mailing list