How come print cannot be assigned to a variable?

Discussion in 'Python' started by anonymousnerd@gmail.com, May 20, 2005.

  1. Guest

    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
     
    , May 20, 2005
    #1
    1. Advertising

  2. Adriano Ferreira, May 20, 2005
    #2
    1. Advertising

  3. Guest

    Thank You Adriano. You were a huge help.

    Vaibhav
     
    , May 20, 2005
    #3
  4. John Doe Guest

    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]
     
    John Doe, May 23, 2005
    #4
  5. 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
     
    Steven Bethard, May 23, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hui
    Replies:
    2
    Views:
    3,051
  2. =?Utf-8?B?cGFhcGEyMQ==?=

    Variable is used before it has been assigned

    =?Utf-8?B?cGFhcGEyMQ==?=, Jan 8, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    1,879
    Karl Seguin [MVP]
    Jan 8, 2006
  3. Jean-Daniel Gamache

    global variable not assigned, newbie question

    Jean-Daniel Gamache, Nov 1, 2004, in forum: Python
    Replies:
    2
    Views:
    311
    Peter Hansen
    Nov 2, 2004
  4. JulioNobre
    Replies:
    0
    Views:
    6,927
    JulioNobre
    Sep 26, 2006
  5. warpcat
    Replies:
    9
    Views:
    277
    Sion Arrowsmith
    May 5, 2009
Loading...

Share This Page