None
in Python is a reserved word, just a question crossed my mind about the exact value of None
in memory. What I\'m holding in my mind is this, <
None
references some Python object of the NoneType
, actually the only instance of its kind. Since it’s a Python object—like literally every other thing—it’s not just the raw content what is stored about also the additional object information like its type.
Every object has this overhead; that’s why integers start with a size of 28 for example. You cannot change this, it’s an implementation detail of Python and its type system.
It turns out that None consumes 16 bytes and that's actually too much for simply an empty value.
None ist not an empty value. It is an NoneType
object which represents the absence of a value as you can read here: Python Built-in constants Doc. You can assign None
to variables, but you can't assign to None, as it would raise an syntax error.
None is a special constant in Python that represents the absence of a value or a null value. It is an object of its own datatype, the NoneType. We cannot create multiple None objects but can assign it to variables. These variables will be equal to one another. We must take special care that None does not imply False, 0 or any empty list, dictionary, string etc. For example:
>>> None == 0
False
>>> None == []
False
>>> None == False
False
>>> x = None
>>> y = None
>>> x == y
True
Void functions that do not return anything will return a None object automatically. None is also returned by functions in which the program flow does not encounter a return statement. For example:
def a_void_function():
a = 1
b = 2
c = a + b
x = a_void_function()
print(x)
Output:
None
This program has a function that does not return a value, although it does some operations inside. So when we print x, we get None which is returned automatically (implicitly). Similarly, here is another example:
def improper_return_function(a):
if (a % 2) == 0:
return True
x = improper_return_function(3)
print(x)
Output:
None
Although this function has a return statement, it is not reached in every case. The function will return True only when the input is even. So, if we give the function an odd number, None is returned implicitly.
None
is singleton object which doesn't provide (almost) none methods and attributes and its only purpose is to signify the fact that there is no value for some specific operation.
As a real object it still needs to have some headers, some reflection information and things like that so it takes the minimum memory occupied by every python object.