How come print cannot be assigned to a variable?

John Doe no at spam.com
Sun May 22 19:55:37 EDT 2005


anonymousnerd at gmail.com wrote:
> Hi all,
>     In Python, some functions can be assigned to variables like this:
>         length=len
> Why is it that print cannot be assigned to a variable like this? (A
> syntax error is declared.)
> 
>   Thanks,
> 
>  Vaibhav
> 

print can't be assigned to variables since it's not a funcion, it's part
of the syntax of the language, like `for', `while', etc. and it can't be
assigned to variables, just as those can't be assigned.
If you need a function that prints stuff, consider these two examples:
[snip]
PrintFunc = lambda x: print x
[snip]
Or, a somewhat better solution:
[snip]
import sys
PrintFunc = sys.write
[snip]



More information about the Python-list mailing list