"print" as function not statement

David MacQuigg dmq at gain.com
Wed Mar 10 10:32:45 EST 2004


On 10 Mar 2004 05:43:22 -0500, aahz at pythoncraft.com (Aahz) wrote:

[snip]

>The other issue is that you're wrong to make print() a function.
>Instead, it should be a callable object.  That way, the default print
>object has state:
>
>    print(a, b, c)
>    print.set_output(my_file)
>    print(a, b, c)
>    print(x, y, z)
>    print.set_separator(None)
>    print("The value is: ", 3)

This style will encourage users to separate the setting of non-default
behavior from the place where that behavior is used.  You won't know
by looking at a print statement what it is going to do.  For
situations like this, where we assume the default behavior (adding
spaces, for example) will only be over-ridden in a few places, I would
rather see the over-ride right where it is used.

show(5, 6, separator = ",")

If this needs to be done throughout the program, and typing the extra
argument gets to be annoying, then I would define a new function with
my intended default.

showWithComma(5, 6)

I would like to change the default behavior of the list class, but
Python (wisely) doesn't let me.  Instead I define a class
MyList(list), and add the methods I want.

-- Dave




More information about the Python-list mailing list