Python Glossary Русский

all Python 3Built-in typesterm

None

special values

The sole instance of NoneType, meaning "no value". A function without return gives it back, it serves as a default value, and it is compared with is.

Syntax

None

None cannot be reassigned and there is exactly one per process, so x is None is exact while x == None depends on someone else's __eq__. A common trap is methods that change an object in place: a = a.sort() stores None in a, and the next a.append() fails with AttributeError "'NoneType' object has no attribute". Do not confuse None with falsehood: if not x: also fires on 0 and on an empty string — if you mean None specifically, write is None.

Examples

def f():
    pass
print(f() is None)   # → True — функция без return
a = [3, 1, 2]
a = a.sort()
print(a)   # → None — sort() меняет список на месте
a.append(4)   # → AttributeError
class Always:
    def __eq__(self, other):
        return True
x = Always()
print(x == None, x is None)   # → True False — == можно переопределить
print(type(None).__name__)   # → NoneType
print(None < 1)   # → TypeError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy