What is print? A function?

Michael Hoffman cam.ac.uk at mh391.invalid
Sun Jan 23 13:04:27 EST 2005


Frans Englich wrote:
> Nah, I don't think it's a function, but rather a builtin "statement". But it's 
> possible to invoke it as an function; print( "test" ) works fine.

That is not invoking it as a function. The parentheses are only for 
ordering the expression on the right

You can do this too:

 >>> print("abc"),("def"),("ghi")
abc def ghi

> So I wonder, what _is_ exactly the print statement?

Uh, a statement.

> The reason I thinks about this is I need to implement a debug print for my 
> program; very simple, a function/print statement that conditionally prints 
> its message whether a bool is true. Not overly complex.
> 
> I tried this by overshadowing the print keyword, but that obviously didn't 
> work.. Is defining a two-liner function the right way to go, or is there 
> better ways to approach it?

In the long run, you might want to look into the logging module. In the 
short run:

def _debug_true(text):
     print >>sys.stderr, text

def _debug_false(text):
     pass

if command_line_debug_option:
     debug = _debug_true
else
     debug = _debug_false

That way you only have to check whether the option is true once in the 
entire run of your program, not every time you call the debug() function 
(which is presumably many times).
-- 
Michael Hoffman



More information about the Python-list mailing list