[print >>] None

Erik Max Francis max at alcyone.com
Tue Oct 23 20:15:10 CEST 2001


Larry Whitley wrote:

> def output(self, fout):
>     print >>fout, self.thing1, self.thing2, self.thing3
> 
> In the calling program I say:
> 
> object.output( fout ) # print to file
> object.output( None) # print to standard output
> 
> But Python complains that None is a variable that has not been
> previously
> set.  Can someone explain?  I thought None was the empty object
> reference.
> Where have I gone astray?

It's not clear to me why you expected this work.  print >> None is not
the same as plain old print.  print >> x requires an (output) file
object, and None isn't one.

Simply check the value of None before you start and set it to stdout if
it's None:

	def output(self, file = None):
	    if file is None:
	        file = sys.stdout
	    print >> file, ...

-- 
 Erik Max Francis / max at alcyone.com / http://www.alcyone.com/max/
 __ San Jose, CA, US / 37 20 N 121 53 W / ICQ16063900 / &tSftDotIotE
/  \ Could it be / That we need loving to survive
\__/ Neneh Cherry
    Erik Max Francis' bookmarks / http://www.alcyone.com/max/links/
 A highly categorized list of Web links.



More information about the Python-list mailing list