473,387 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How come print cannot be assigned to a variable?

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

Jul 19 '05 #1
4 1520
print is a statement, not a function.

Read Guido's words on that:
http://www.python.org/search/hyperma...1992/0112.html

Regards.
Adriano.
Jul 19 '05 #2
Thank You Adriano. You were a huge help.

Vaibhav

Jul 19 '05 #3
an***********@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]
Jul 19 '05 #4
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
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Karl Scalet | last post by:
Hi, quite often there is a need to just print out the items of a list. that would be nice, except prt() does not exist, and print is a statement not a function, hence cannot replace prt as...
23
by: stewart.midwinter | last post by:
No doubt I've overlooked something obvious, but here goes: Let's say I assign a value to a var, e.g.: myPlace = 'right here' myTime = 'right now' Now let's say I want to print out the two...
2
by: Trevor Davies | last post by:
I have a database which I have loaded onto our as/400 v4r5. All the tables and data have loaded successfully. I am using DB2 connect to access. I now want to delete the data in a table which is...
2
by: hui | last post by:
Here is a problem I am having with web form designer. I have a database control in the form, and setup the connection string as a dynamic property. It compiles and runs fine. I close the aspx...
4
by: hon123456 | last post by:
Dear all, I am writing ASP page, and I have create many session variable. Is there exist a function or method to print out ( output ) all the session variable in one page. Because I need to...
10
by: Tony K | last post by:
How would I assign the result of a SQL query to a variable. The result of the following statement will ALWAYS result in 1 row returned. SELECT ProductDescription, ProductID FROM ...
3
by: Max58kl | last post by:
Trying to access data and print it to the screen using Perl Builders I/O Window -------------------------------------------------------------------------------- Hi I am using a program called...
1
by: Scott M. | last post by:
Many methods return objects when they are called. With Excel these objects are placed in memory and must be destroyed via ReleaseComObject as you have done with your NAR method, but the line: ...
3
by: Tim | last post by:
Folks, I'm trying to format a print string so that it reports progress whilst processing a looping structure with a date time stamp appended to the end of the string. This started out life as a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.