How come print cannot be assigned to a variable?
Steven Bethard
steven.bethard at gmail.com
Sun May 22 23:45:19 EDT 2005
John Doe wrote:
> If you need a function that prints stuff, consider these two examples:
> [snip]
> PrintFunc = lambda x: print x
py> printfunc = lambda x: print x
Traceback ( File "<interactive input>", line 1
printfunc = lambda x: print x
^
SyntaxError: invalid syntax
See Inappropriate use of Lambda on the Python Wiki[1]. This is also a
horrible idea as it means you can only print a single item:
py> print 1, 2, 3
1 2 3
py> def write(x):
... print x
...
py> write(1, 2, 3)
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
TypeError: write() takes exactly 1 argument (3 given)
py> def write(*args):
... print args
...
py> write(1, 2, 3)
(1, 2, 3)
The print statement has special spacing behavior. You could perhaps
approximate it with something like:
py> def write(*args):
... for arg in args:
... print arg,
... print
...
py> write(1, 2, 3)
1 2 3
But why go to all this effort?
STeVe
[1] http://wiki.python.org/moin/DubiousPython
More information about the Python-list
mailing list