[print >>] None

Steven D. Majewski sdm7g at Virginia.EDU
Wed Oct 24 21:45:54 CEST 2001


On Wed, 24 Oct 2001, Huaiyu Zhu wrote:

> For quite some time I've been using one object for all these sort of things,
> including terminating linked lists, etc.  
> 
> class Nothing:
>     def __str__(self): return ""
>     def __repr__(self): return "nothing"
>     def __call__(self, *args, **keys): return nothing
>     def __len__(self): return 0
>     def __nonzero__(self): return 0
>     def __setitem__(self, i, x): raise IndexError
>     def __getitem__(self, i): raise IndexError
>     def __setattr__(self, name, val): return nothing
>     def __getattr__(self, name): return nothing
> 
> nothing = Nothing()
> 
> if __name__ == "__main__":
>     for x in nothing: print x


Note that in 2.1, the above is equiv. to iterating over an empty sequence.
In 2.2, it's equivalent to iterating over an infinite sequence. 

My guess is that is has something to do with __getattr__ returning
<nothing> ... possibly on trying nothing.__iter__ and/or nothing.next ? 


Adding a next method seems to work:

>>> class Null(Nothing):
...     def next(self):
...             raise StopIteration
... 
>>> nothing= Null()
>>> for i in nothing: print i
... 
>>>


-- Steve Majewski





More information about the Python-list mailing list