
On Mon, Jul 23, 2018 at 8:08 AM Grégory Lielens <gregory.lielens@gmail.com> wrote:
On Monday, July 23, 2018 at 8:24:45 AM UTC+2, Nicholas Cole wrote:
And that leads to a simple question: how many times does this actually occur in real-world by python code? -- i.e. how many times do I want to check the value of an existing label, and, finding it is None (and None specifically), then assign it a value?
The PEP present a few examples.
I think the compactness and clarity is really gained when doing a descent into an "attribute tree", where any (or selected) members can be None, like getting back to the example by Steven (spell-corrected, and expanded):
meal = obj?.spam?.eggs.tomato?.cheese ?? "Mozzarella"
Where, to put the proposal in even better light, I have assumed that eggs instances always have a tomato attributes (i.e. are never None).
This is indeed much more compact that the current version, and arguably more readable (it's easier to parse once you know the syntax...but you have to know the special syntax that will be used for this kind of stuff only). The more you deepen the attribute access, the more you gain, but of course deep attribute access is not so common. The new operators may make deep attribute hierarchies (or deeply nested dicts/lists) slightly more common, and there also I do not think it's a good thing.
That above example looks terrible to read (to me). Yes, that's a subjective statement. This example from the PEP is even worse: Example: if entry.is_dir(): dirs.append(name) if entries is not None: entries.append(entry) else: nondirs.append(name) After updating to use the ?. operator: if entry.is_dir(): dirs.append(name) entries?.append(entry) else: nondirs.append(name) In the first, it's totally clear to me when entries would be appended to and when it wouldn't. The second I had to read several times before I could see what was going on. The fact that it is already embedded in an if...else statement perhaps made it harder to understand. I guess people will say that we will just get used to the new syntax, but I don't see the benefit of making that particular "if" statement more compact, and leaving all the others in place. Or to put it another way, I'm just not convinced that "None" is sufficiently special. N.