What is print? A function?

Fredrik Lundh fredrik at pythonware.com
Sun Jan 23 13:04:19 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.
>
> So I wonder, what _is_ exactly the print statement? The untraditional way of
> invoking it(without paranteses) makes me wonder.

it's a statement.  (expr) is an expression (in parenthesis), so when you
type

    print("test")

python sees:

    print ("test")

(see the language reference for details)

> 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

yup.  something like this might be useful:

    def printif(cond, fmt, *args):
        if cond: print fmt % args

</F> 






More information about the Python-list mailing list