Live data from Hacker News

PEP 505: Bringing None-Aware Operators to Python

python.org

71–80 of 80 posts

Re: PEP 505: Bringing None-Aware Operators to Python

#71
post #58

Earlier quoted context omitted.

It can already be written nicely as libpaths = paths.split(":") if paths else [] Please don't adopt unnecessary sigil junk from other languages.

This is subtly incorrect. Correctly, it should be: libpaths = paths.split(":") if paths is not None else [] or else falsey values of paths (0, [], False) can still cause an error.

If your path variable is set to 0, [], False then it's best to fail early anyways.

Re: PEP 505: Bringing None-Aware Operators to Python

#72

Earlier quoted context omitted.

It can already be written nicely as libpaths = paths.split(":") if paths else [] Please don't adopt unnecessary sigil junk from other languages.

I think a better example to show why you might want a ?. operator would involve nested lookups. If you have something like node.child.attribute.value, handling potential Nones at each lookup with ifs would get very verbose very quickly (and that verbosity would be bad, as it would make it much harder to make out the main point of the code). You could surround the nested lookups in try/except and catch the AttributeEr…

I don't think the existing pythonic way is not too verbose:

   if node and node.child and node.child.attribute: 
      …

Re: PEP 505: Bringing None-Aware Operators to Python

#73

Earlier quoted context omitted.

It can already be written nicely as libpaths = paths.split(":") if paths else [] Please don't adopt unnecessary sigil junk from other languages.

I think a better example to show why you might want a ?. operator would involve nested lookups. If you have something like node.child.attribute.value, handling potential Nones at each lookup with ifs would get very verbose very quickly (and that verbosity would be bad, as it would make it much harder to make out the main point of the code). You could surround the nested lookups in try/except and catch the AttributeEr…

for nested loop you can do

   for child in node.children or []:
      for attrib in child.attributes or []:
         …

Re: PEP 505: Bringing None-Aware Operators to Python

#74

I am quite concerned about the kinds of PEPs that come up lately (edit: I now noticed it's three years old). Python is drifting farther and farther away from its nature and strength. The amount of complexity is constantly going up and I don't think it's a good thing. Please let this language be what is is. A small amount of boilerplate in cases like this is fine and motivates you to restructure the code rather than h…

Adding to this comment -

    data = data if data is not None else []
(is)

    data = data if data else []
(is)

    data = data or []
which means you either did:

    data = []
(or)

    data = None
The line above is wrong - you always define your variables to match your structures properly during your __init__.

For all intents and purposes, [] is the same as None when you're attempting a true/false if statement (of course it's not 100% but I'm trying to make the point).

Having a Null coalescing operator is a cheap and crappy way to escape proper variable definition and proper statement resolution, and it means you don't understand what your code actually does when reaching the compiler, or behavior in the memory. PEP 505 should be rejected.

Re: PEP 505: Bringing None-Aware Operators to Python

#76

I am quite concerned about the kinds of PEPs that come up lately (edit: I now noticed it's three years old). Python is drifting farther and farther away from its nature and strength. The amount of complexity is constantly going up and I don't think it's a good thing. Please let this language be what is is. A small amount of boilerplate in cases like this is fine and motivates you to restructure the code rather than h…

> data or [] Unfortunately this kind of conditional assignment does not work if `data` is a numpy array. This arguably more numpy's fault than python's, though it is a mildly infuriating when it happens.

Re: PEP 505: Bringing None-Aware Operators to Python

#77

Earlier quoted context omitted.

I think a better example to show why you might want a ?. operator would involve nested lookups. If you have something like node.child.attribute.value, handling potential Nones at each lookup with ifs would get very verbose very quickly (and that verbosity would be bad, as it would make it much harder to make out the main point of the code). You could surround the nested lookups in try/except and catch the AttributeEr…

for nested loop you can do for child in node.children or []: for attrib in child.attributes or []: …

That's generally considered unpythonic though. A more pythonic way to write that is

    for child in node.children if node.children else []:
        for attrib in child.attributes if child.attributes else []:
            ...
Which, granted, is a lot more verbose and probably not any more readable but at least it's not abusing the or operator.

Re: PEP 505: Bringing None-Aware Operators to Python

#78
post #37

It will be interesting to see how this PEP is handled by the community with Guido off on a permanent vacation, and the mood among Python core devs seemingly "no major changes to syntax for the next little while" . Personally, some of the examples using the new operators are in fact easier to read, but others border on illegibility, and I don't think the tradeoff is wise. Then again, I felt the same way about Python's…

> It will be interesting to see how this PEP is handled by the community It was created three years ago, and it's still just a draft. Hopefully it won't go anywhere, with or without Guido around. EDIT: Raymond Hettinger is on the ball as always: "This PEP also shares some traits with PEP 572 in that it solves a somewhat minor problem with new syntax and grammar changes that affect the look and feel of the language in…

And yet, PEP 572 was recently accepted...

Re: PEP 505: Bringing None-Aware Operators to Python

#79
post #59

We have a lot of Django-based code similar to if getattr(obj, 'foreign_obj') is not None and getattr(obj.foreign_obj, 'field_name') is not None: Would this turn into: if obj?.foreign_obj?.field_name ? Does is look shorter? - YES!. Is it more readable? Arguably yes. Would I vote to see this feature in Python? HELL NO! In most of cases, we can have an in-house "maybe()" function, like maybe(obj, 'foreign_obj.field_name…

...why does everyone forget that `getattr()` has an optional `default` argument? It's helpful for stuff like this.

Same reason people forget min and max have key arguments: You mostly don't need them until you really do.

Re: PEP 505: Bringing None-Aware Operators to Python

#80
post #31
post #23

Earlier quoted context omitted.

The whole endeavor seems a bit quixotic to me. This kind of change would no doubt cause such a stir that there’d be a fork before long.

No, it's not that drastic, especially considering prior art in other languages Not supporting the change, but let's not be dramatic

I'm fairly well attuned to the sensibilities of Pythonistas and between this and the recent stepping down of the BDFL, it would definitely invite a fork, fuelled in no small part by the vacuum that exists in spearheading the Python project.
Post reply on HN