Programming Search⌘K

Featureintro

None, NotImplemented and Ellipsis

Also called Python None, NoneType, NotImplemented constant, Ellipsis, three dots literal, Python singletons.

None, NotImplemented and Ellipsis are the only values of their types. None marks an absent value, NotImplemented is returned by operator methods that give up, and ... marks an omission.

Language
Python

Synopsis

def nothing():
    pass

print(nothing() is None, type(None).__name__)
print(None is type(None)(), Ellipsis is ..., type(...).__name__)
print(bool(None), bool(...))
Output
True NoneType
True True ellipsis
False True

A function without a return gives None. Each of the three types has one object, and calling the type gives that object again.

Semantics

r1. Each of the three types "has a single value. There is a single object with this value."1 So x is None is the test for None. Example: three-singletons.

r2. None signifies "the absence of a value in many situations", for example the result of a function that does not return anything explicitly. Its truth value is false.1 It is often used for a default argument that the caller did not pass, and an assignment to None raises a SyntaxError.2

r3. Numeric methods and rich comparison methods should return NotImplemented when they do not implement the operation for the given operands. The interpreter then tries the reflected operation, or another fallback that depends on the operator.1 If all attempts return NotImplemented, the interpreter raises an exception.2 Example: not-implemented.

r4. NotImplemented should not be used in a Boolean context. Such a use was deprecated first, and Python 3.14 made it raise a TypeError.1

r5. Ellipsis is "accessed through the literal ... or the built-in name Ellipsis", and its truth value is true.1 It is "frequently used to indicate that something is omitted". The name Ellipsis can be assigned to, but an assignment to ... raises a SyntaxError.2 Example: placeholder-body.

Examples

__add__ returns NotImplemented for an operand that it does not know. For 0 + Meters(1), the first call made by sum(), int.__add__ gives NotImplemented and Python calls Meters.__radd__ (rule r3). For Meters(1) + "x", both sides give up, so Python raises TypeError.

class Meters:
    def __init__(self, v):
        self.v = v
    def __add__(self, other):
        if not isinstance(other, Meters):
            return NotImplemented
        return Meters(self.v + other.v)
    def __radd__(self, other):
        if other == 0:
            return self
        return NotImplemented

print(sum([Meters(1), Meters(2)]).v)
try:
    Meters(1) + "x"
except TypeError as e:
    print("TypeError:", e)
Output
3
TypeError: unsupported operand type(s) for +: 'Meters' and 'str'

... is a valid body for a function that is not written yet (rule r5). The function returns None.

def todo():
    ...

print(todo(), ... is Ellipsis)
Output
None True

Pitfalls

if not x for None

0, "" and [] are false too, so not x does not tell None apart from them (rule r2). Use x is None.

This code runs but gives a wrong answer on purpose.

v = 0
print(v is None, not v)
Output
False True

Raising NotImplemented

NotImplemented and the exception NotImplementedError "are not interchangeable".2 Return NotImplemented from an operator method. Raise NotImplementedError from a method that a subclass must override.

The fallback of ==

If __eq__ returns NotImplemented on both sides, == does not fail. It falls back to identity. By default, object implements __eq__() "by using is".3

This code runs but gives a wrong answer on purpose.

class C:
    def __eq__(self, other):
        return NotImplemented

c = C()
print(C() == C(), c == c)
Output
False True

Comparison

Go has no single object for a missing value. A variable without an initial value gets the zero value of its type: "false for booleans, 0 for numeric types, "" for strings, and nil for pointers, functions, interfaces, slices, channels, and maps".4 An int in Go cannot be nil. In Python, any name can refer to None (rule r2).

References

  1. The Python Language Reference, Python 3.15, 3.2.1. None, 3.2.2. NotImplemented, 3.2.3. Ellipsis
  2. Built-in Constants, Python 3.15 documentation, None, NotImplemented, Ellipsis
  3. The Python Language Reference, Python 3.15, 3.3.1. Basic customization, object.__eq__()
  4. The Go Programming Language Specification, The zero value

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW3 standard
  • a history with a source