
Big +1 from me. I've been looking forward to having None-aware operators in Python as I find them a very neat language addition. For me personally the main advantage of having maybe-dot (?.) operator is the ability to express certain logic in a much clearer way, for example:
class User(DBModel): phone: str | None
class Publisher(DBModel): owner: ForeignKey[User] | None
class Book(DBModel) publisher: ForeignKey[Publisher] | None
Imagine wanting to get the phone number of the person that published a certain book from the database. In this situation, with maybe-dot operator I can write:
phone_number = book.publisher?.owner?.phone
Getting None value could mean that the book has not been published yet (no publisher relation), owner does not exist or does not have a phone number associated with it. Either way it doesn't matter because the only thing that we wanted to retrieve is the phone number and not the whole context of why it has a certain value and not the other. Personally I find this syntax much more readable than other, "more explicit" expressions like:
phone_number = book.publisher.owner.phone if (book.publisher is not None and book.publisher.owner is not None) else None
Best regards, On Thu, Oct 14, 2021 at 7:37 PM Doug Swarin <dswarin@gmail.com> wrote:
Hello,
I've been following PEP 505 (https://www.python.org/dev/peps/pep-0505/) since it was first proposed for Python 3.8. It's been deferred for some time and I'm interested in seeing it in Python 3.11, but I know there were also a number of objections which resulted in it being deferred (including by one of the original authors, Mr. Dower). I did email both Mr. Dower and Mr. Haase and they graciously gave me their permission to bring it up on this list for discussion and hopefully final pronouncement one way or the other.
I personally believe that the PEP will result in a significant reduction in boilerplate code, and it is substantially similar to the same operators now found in a number of other languages, especially C# and JavaScript ( https://wikipedia.org/wiki/Null_coalescing_operator and https://wikipedia.org/wiki/Safe_navigation_operator).
I believe strong and valid arguments can be made about the use of None being a fundamental flaw in some types of coding (and that adding additional support for it to the language will increase the use of None in this way), but I also believe there are many use cases in programming where it is by far the simplest way to express various semantics, and the fact exists that None is already used extensively in large quantities of code, and further that there is already a great deal of code written to constantly test against None and break out of a statement without throwing an error.
I also understand the argument that especially the maybe-dot (?.) and maybe-subscript (?[) operators can decrease readability of code and also believe these are valid arguments against it. While I believe the existence and use of these operators in other languages definitely helps the case that these can be used and understood successfully, I think it is entirely valid to either consider other syntax (though I prefer the chosen syntax of PEP 505), or even to reduce PEP 505 to having only the coalesce operator (??) and the maybe-assign operator (??=).
Separately, I have implemented a pure-Python solution for PEP505 (which is definitely rather beta) which might help test the waters for a final implementation in CPython (though the CPython implementation would of course be much more efficient). It can be found at https://pypi.org/project/pep505/
Thanks, Doug _______________________________________________ Python-Dev mailing list -- python-dev@python.org To unsubscribe send an email to python-dev-leave@python.org https://mail.python.org/mailman3/lists/python-dev.python.org/ Message archived at https://mail.python.org/archives/list/python-dev@python.org/message/XZZIV42X... Code of Conduct: http://python.org/psf/codeofconduct/