4

The python language reference mentions three objects which have a single, unique instance: None, NotImplemented, and Ellipsis (section 3.2 "The Standard Type Hierarchy"). The test x is None is a common idiom made possible by the guaranteed uniqueness of None. I cannot find any other mention of unique-instance objects, either in python documentation or on stack overflow. A few questions, like this one have interesting suggestions for ways to construct such objects, but what I want to know is whether there are any beyond these three built-ins that I've missed.

For example, () appears to be unique (in admittedly very limited testing in CPython), so is the test x is () safe? Or is x == () mandatory for this case?

Community
  • 1
  • 1
David Munro
  • 273
  • 1
  • 8
  • It makes no sense to test for `x is ()`, `if x` would achieve the same. Why are you actually asking? – Padraic Cunningham Dec 13 '15 at 19:52
  • There are many possible values for which `bool(x)` is `False`. I needed to check whether `x` was actually `()`, not one of the other possibilities, for exactly the same reason that you use `x is None`. – David Munro Dec 13 '15 at 20:54
  • not if x is a tuple, `isinstance(x, tuple)` would also explicitly allow you to check that x was in fact a tuple – Padraic Cunningham Dec 13 '15 at 20:59
  • It was helpful to see this and realize that I could also return NotImplemented instead of None (and test it using 'is') when I want to signal that something doesn't really exist when None is a valid value (i.e. the object being requested from a function/method exists, but it can have the value of None vs the object not existing) – wojtow May 14 '16 at 19:38

3 Answers3

2

Well... you can think of any global object as being "unique" in the sense that it is always the same object.

For example:

>>> x = file
>>> x is file
True
>>> x = True
>>> x is True
True
>>> x = ZeroDivisionError
>>> x is ZeroDivisionError
True

There is nothing special about:

>>> x = None
>>> x is None
True

What you are probably wondering is why you should test x is None rather than x == None.

The answer is: x == None will call x.__eq__(None) which may return True in all sorts of situations where x is not actually None (although in reality that is not very common).

Instance?

Well, you may say there is a difference: None is an instance.

Well, the truth is, everything in Python is an object, which means everything is an instance. In my examples, file and ZeroDivisionError are instances of type, True is instance of bool and None is instance of NoneType (note that type is an instance of type, so there are no exceptions to the rule).

There is one special thing about None - it is the only instance of NoneType and you cannot make others (unless there is some good trick?)

But that is actually a property of NoneType - it does not let you make new instances:

TypeError: cannot create 'NoneType' instances

But that is not really essential for the whole story.

EDIT These "unique-instance objects" are called singletons. You can make some of yours, of course.

zvone
  • 18,045
  • 3
  • 49
  • 77
1

For what is worth, () is not unique in PyPy 4.0.1:

>>>> x = ()
>>>> x is ()
False
Miikka
  • 4,573
  • 34
  • 47
1

Nope, there are no other built in types that have single values. The only relative of these singletons is the bool type which allows for two values (namely True and False)*.

In Python 2.x (I cannot find them in 3.x) you can see this limitation by importing NotImplementedType, EllipsisType, NoneType from the types module and realize that it is explicitly not with it's fancy custom TypeError:

In [13]: from types import NoneType

In [14]: n = NoneType()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-b386f51143fb> in <module>()
----> 1 n = NoneType()

TypeError: cannot create 'NoneType' instances

() is unfortunately not unique, it represents an empty tuple, if the type Tuple was unique in the sense that NoneType is unique then all its instances would require to have the same value.


If you want to take a closer look at these, they are located in Objects/object.c in lines 1351 and 1473 for None and NotImplemented respectively. Ellipsis is yet to be found.


*Update: Apparently, True and False are also singletons, according to PEP 285 - Adding a bool type:

The values False and True will be singletons, like None. Because the type has two values, perhaps these should be called "doubletons"? The real implementation will not allow other instances of bool to be created

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253