Every now and then I come across code like this:
foo = Foo()
...
if foo.bar is not None and foo.bar.baz == 42:
shiny_happy(...)
Which seems, well, unpythonic, to me.
In Objective-C, you can send messages to nil and get nil as the answer. I’ve always thought that’s quite handy. Of course it is possible to implement a Null pattern in Python, however judging from the results Google’s given me, it seems this is not very widely used. Why’s that?
Or even better—would it be a bad idea to let None.whatever return None instead of raising an exception?
PEP 336 – Make None Callable proposed a similar feature:
The reason for why it was rejected was simply “It is considered a feature that None raises an error when called.”