4 ms·
Null is an interesting exception in languages with static typing such as Java. But there is no static typing in Python. Literally any variable can hold any valu
by mikeash 15y ago
Null is an interesting exception in languages with static typing such as Java. But there is no static typing in Python. Literally any variable can hold any value of any type. None is not nearly so special there, since you can also assign 3 to any variable, or the identity function, or a string containing the answer to the question of life, the universe, and everything.
In languages like Java, null has special semantics that no other value has. In Python, there's nothing particularly special about None, it's simply used as a convenient sentinel by convention. I could easily write Python code that returns the literal string "This value is empty" as the sentinel instead, something that doesn't apply to languages like Java.
- whateverer 15y agoYes, actually, you are right. It's all the same in Python because the references can always point to any value, and None is just a value of the NoneType. And due to Python's runtime types, it wouldn't matter either way, because even if None only existed within the context of a Maybe type, you could still just apply any operation on it and have crash at the first method call on it.
- SoftwareMaven 15y agoThat's not completely accurate. None is still special in Python because it's the value chosen by the interpreter to represent an expression that doesn't explicitly return a value. It is true that None is implemented like any other object (whther the value of 5 or an instance of my own class), but it still maintains special semantics.