|
|
Subscribe / Log in / New account

Not coalescing around None-aware

Not coalescing around None-aware

Posted Dec 23, 2022 9:17 UTC (Fri) by ceplm (subscriber, #41334)
In reply to: Not coalescing around None-aware by shironeko
Parent article: Not coalescing around None-aware

My problem with these operators (and a lot of what already happened to Python in the last few releases) is that it is similar to the Java discussion on the checked exceptions: it is completely dominated by people who didn’t understand the problem and they are trying to get rid of something which they consider just an annoyance.

For me all those if foo is None are code smell: a sign of poorly created API (perhaps unavoidable, because introduced from an external library). Similarly to the checked exceptions, when you start to look for improved syntax which would get rid of if is None then reconsider whole code around the problem and you may find that you need restructure it to express your algorithm better.


to post comments

Not coalescing around None-aware

Posted Dec 23, 2022 11:05 UTC (Fri) by adobriyan (subscriber, #30858) [Link] (1 responses)

There is Python gotcha where default class member values are global:

class X:
l = []

x1 = X()
x2 = X()
print(x1.l, x2.l)
x1.l.append(1)
print(x1.l, x2.l)

results in

[] []
[1] [1]

so passing None as default value in constructor is obvious solution. I'm not sure how often this is seen in practice but not code smell for sure.

Not coalescing around None-aware

Posted Dec 23, 2022 11:49 UTC (Fri) by smurf (subscriber, #17840) [Link]

> not code smell for sure

Well, "language smell" then – as there really should be a way to instantiate the default option(s) at the time they are needed, not when the "def" statement is compiled.

Attempts to add a way to do this to Python have not succeeded so far.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds