pep 336: Make None Callable

Peter Otten __peter__ at web.de
Thu Nov 4 05:01:24 EST 2004


The Eternal Squire wrote:

> None should be a callable object that when called with any
> arguments has no side effect and returns None.

Why would you stop there? 

>>> class BusyNone(object):
...     def __call__(self, *args, **kw):
...             return BusyNone
...     __getattr__ = __getitem__ = __add__ = __call__
...     def __str__(self):
...             return "BusyNone"
...     __repr__ = __str__
...
>>> BusyNone = BusyNone()
>>> BusyNone("a", 1)
BusyNone
>>> BusyNone[1:2]
BusyNone
>>> BusyNone.busyAttr
BusyNone
>>> BusyNone + 42
BusyNone
>>> len(BusyNone)
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: len() of unsized object

Still room for improvement, but you should get the idea...

Peter




More information about the Python-list mailing list