What is print? A function?

F

Frans Englich

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.

So I wonder, what _is_ exactly the print statement? The untraditional way of
invoking it(without paranteses) makes me wonder.

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?


Cheers,

Frans
 
M

Michael Hoffman

Frans said:
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:
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).
 
F

Fredrik Lundh

Frans said:
I find this a nice solution. The most practical would be if it was possible to
do this with print, of course. But print won't budge.

you can disable print, though:

class dev_null:
def write(self, text):
pass
sys.stdout = dev_null()

or pipe all print requests to a debug stream:

if my_debug_flag:
debug = sys.stdout
else:
debug = dev_null()

print >>debug, "hello"
print >>debug, "the value is", value
Is it possible to create own statements, such that it would be possible to do:

printDebug "test"

no.

</F>
 
F

Frans Englich

Frans Englich wrote: [...]
if command_line_debug_option:
debug = _debug_true
else
debug = _debug_false

I find this a nice solution. The most practical would be if it was possible to
do this with print, of course. But print won't budge.

Is it possible to create own statements, such that it would be possible to do:

printDebug "test"

?


Cheers,

Frans
 
S

Steven Bethard

Frans said:
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.

Sounds like you want to override sys.stdout:

py> class ConditionalWriter(object):
.... def __init__(self, outfile, cond=True):
.... self.outfile = outfile
.... self.cond = cond
.... def write(self, text):
.... if self.cond:
.... self.outfile.write(text)
....
....
py> import sys
py> writer = ConditionalWriter(sys.stdout)
py> sys.stdout = writer
py> print "hi!"
hi!
py> writer.cond = False
py> print "hi!"
py> writer.cond = True
py> print "hi!"
hi!

You probably want to restore sys.stdout when you're done too:

py> sys.stdout = writer.outfile
py> writer.cond = False
py> print "hi!"
hi!

That said, you probably _really_ want the logging module:
http://docs.python.org/lib/module-logging.html

Steve
 
R

Roy Smith

Frans Englich said:
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's not calling it as a function. The parens in this case are simply
evaluated as grouping operators around the string literal.
So I wonder, what _is_ exactly the print statement? The untraditional way of
invoking it(without paranteses) makes me wonder.

It's a statement, just like "write" in Fortran. When C came around, the
idea of a language having no built-in print statement and having to call
a function to generate output was "untraditional". The function was
named "printf" (with an "f" at the end) to emphasize that it was a
function call, not a built-in language keyword. Java, and many other
quasi-C-based languages also use print functions, and this has become so
common that people have come to expect it. It's even a function in
Perl, although that language's devil-may-care attitude about punctuation
makes it difficult to tell for sure :)
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.

You can certainly define a function which conditionally calls print, you
just can't call it "print", because that's a reserved word. But, before
you get too far down that path, you might want to explore the logging
module. It suffers from a bit of kitchen-sink syndrome, so it may be
more complicated than you want to deal with, but it's worth taking a
look at. You may discover that what you want to do is already done for
you.
 
G

Georg Brandl

Roy said:
It's a statement, just like "write" in Fortran. When C came around, the
idea of a language having no built-in print statement and having to call
a function to generate output was "untraditional". The function was
named "printf" (with an "f" at the end) to emphasize that it was a
function call, not a built-in language keyword.

I beg to differ. Doesn't 'printf' stand for 'PRINT Formatted string'?
Java, and many other
quasi-C-based languages also use print functions, and this has become so
common that people have come to expect it. It's even a function in
Perl, although that language's devil-may-care attitude about punctuation
makes it difficult to tell for sure :)

And it should have been and perhaps will be a function in Python, see
"Python Regrets" by GvR.

Georg
 
N

Nelson Minar

Frans Englich said:
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.

As several folks have said, print is a statement, and Python doesn't
really let you add statements to the language.

You said you wanted to build a simple debugging function, so maybe
this doesn't fit the bill, but for bigger pieces of code Python has a
nice logging library. It's something like log4j or java.util.logging.
http://www.python.org/doc/2.4/lib/module-logging.html
 
D

david.tolpin

Is it possible to create own statements, such that it would be possible to do:

printDebug "test"

?
This question is well addressed in a neighbour group comp.lang.lisp .
 
S

Sion Arrowsmith

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

You can do this too:

abc def ghi

And for further enlightenment:

..>>> print("abc", "def", "ghi")
('abc', 'def', 'ghi')
 
R

Ryan Paul

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.

So I wonder, what _is_ exactly the print statement? The untraditional
way of invoking it(without paranteses) makes me wonder.

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?


Cheers,

Frans

Unfortunately, it isnt possible to add new kinds of statements to python.
you might want to have a look at Ruby, in which paren are entirely
optional. Ruby is similar to python in a lot of ways, but its also quite
different. It provides lots of syntactic sugar and a superior object
model, but its a resource hog compared to python.

You may also be interested in Logix. Logix is a language framework built
on top of python that allows you to dynamically extend python syntax at
run-time. Unfortunately, Logix is still rather new, and its incredibly
slow.

http://logix.livelogix.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,217
Messages
2,571,121
Members
47,724
Latest member
Farreach2565

Latest Threads

Top