[print >>] None

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Wed Oct 24 15:07:35 EDT 2001


On 23 Oct 2001 23:10:39 -0400, Barry A. Warsaw <barry at zope.com> wrote:

>Neither was I <wink>.  I lobbied for "print >> None" to be morally
>equivalent to:
>
>class Devnull:
>...  def write(self, s): pass
>... 
>>>> devnull = Devnull()
>>>> print >> devnull, 'this goes no where'
>>>> 
>
>but lost that argument.  I've never had a use for current
>print>>None's semantics but I've had one or two situations where
>printing to a null sink was handy (e.g. doing verbose prints that get
>turned off with a --quiet flag).
>

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__":
    print nothing is nothing
    print nothing
    print `nothing`
    print nothing(1, 2, a=3, b=4)
    nothing.abc = 123
    print nothing.abc
    print >> nothing, "This goes nowhere"
    if nothing: print 456
    for x in nothing: print x
    nothing[0] = 3


nothing-beats-it-ly y'rs

Huaiyu



More information about the Python-list mailing list