print a function by default in Python 2.7.11? [duplicate] - python

This question already has answers here:
Why is parenthesis in print voluntary in Python 2.7?
(4 answers)
Closed 6 years ago.
The other day I accidentally wrote
print("a function?")
in my Python 2.7.11 console and was quiet astonished that it would work instead of throwing an error. I assumed, there was an implicit
from __future__ import print_function
and tried
print "also a statement???"
which also worked! Note that, when importing from __future__ the statement is disabled. It is in fact disabled, and only the function sytax works if I import print_function
I couldn't find anything in the documentation, the Python docs still read:
Note: This function is not normally available as a built-in since the
name print is recognized as the print statement. To disable the
statement and use the print() function, use this future statement at
the top of your module: (...)
What did I miss? Why is print a statement and a builtin function in Python 2.7?

Note that the string "a function?" is an expression in Python, and an expression in parentheses is also an expression.
So your command was print ("a function?"), just printing an expression.
That is convenient for writing a line that works in both Python 2.x and in Python 3.x. The book "Python Crash Course" uses this to show code that works in both versions of Python.

Related

Is there a way to compile the Python code ignore syntax?

I am making code that works on both Python 2 and Python 3.
But there was no problem in theory, but there was a Python problem.
Now I'm build and use both Python 2.7.5 and Python 3.7.4.
This is part of my code
ex)
if sys.version_info < (3,):
print(keys),;
print(values)
else:
print(keys,'/ ', end='')
print(values)
This code that checks Python version with sys.version, corresponding 'if' will be working.
But, of course there is a syntax error.
Python 2 does not support [end=''].
In my opinion...
Even if you actually ignore it and act on it, there's no problem code.
I tried 'Try-except', but syntax errors were not ignored.
How can both Python2 and Python3 not change lines while weaving compatible codes?
Import the package print_function and try
from __future__ import print_function
In this particular case, just get the Python 3 print function in both Python 2 and Python 3 by adding:
from __future__ import print_function
to the very top of your file, then only use the Python 3 syntax.
As for avoiding the SyntaxError from actually incompatible constructs that can't be fixed with a __future__ import, the only solutions are putting the incompatible code in separate modules (a public module can do version testing to import the implementations from the private module appropriate to the Python version), or evaling a string containing the code for the appropriate version (exec won't typically work, because it also changed from keyword statement to built-in function in the transition; eval+compile is the same in both though).
There is no way to just "turn off syntax checking", because invalid syntax definitionally means the parser has encountered an unrecoverable error; you don't want it to try to stumble onwards, guessing at what everything else means in the context of the garbage state it was left in.

Reposurgeon gives me a SyntaxError when using exec, why?

Using reposurgeon and trying to extend its functionality, I am faced with:
reposurgeon: invalid syntax in extension function
which translates to a SyntaxError extension raised from the execfile() call in RepoSurgeon.do_exec(). What gives? The code I am trying to exec is as simple as:
print "Hello world"
I have also used the Python CLI and execfile and there are no complaints whatsoever?
Used version: reposurgeon 3.10
This one took me a while to figure out, which is why I am posting it here.
The key is indeed in the single line of code we're trying to "source". While this is perfectly valid Python 2.x code, reposurgeon uses the print function from Python 3.x by doing:
from __future__ import print_function
Which causes print to require the use of parentheses, as it makes print a function instead of a statement.
Obviously we're running our extension code in the context of reposurgeon, which means that we're dependent on the rules it defines.
See this document.
Hence the following will work just fine:
print("Hello world")

Python 'print' statement and arguments

I'm an experienced C++ developer, but am just getting my feet wet with Python for a project at work. I'm having some basic problems and Google has been less than helpful. I suspect something about my environment is funny, but I have no clue how to diagnose it. I wrote a simple script that merely prints an argument to the screen, but I am getting an error when running it (python args.py):
Syntax Error: invalid syntax
File "args.py", line 4
print arg0
For reference, there is a carrot underneath the 0 of arg0. The script in question:
import sys
firstArg = sys.argv[0]
print firstArg
I'm sure this is something really dumb, but Python is such a foreign thing, coming from C++.
This seems pretty obvious. The traceback says you have a SyntaxError on line 4, therefore print firstArg isn't valid Python.
The not obvious part is that a lot of examples on the Internet use syntax like that. This is because in Python 2.X versions, print was a statement. Since Python 3.0, print is a function. Try print(firstArg) instead.
Protip: If you want to enable the print function in Python 2, use from __future__ import print_function.
I can understand that the print function of Python may be different for C or C++ developers because of its features.
The syntax that you use is for python 2 (now in End Of Life status). There are many differences between Python 2 and python 3.
In this case, the print was a statement in Python 2 but function in Python3.
Here is the correct syntax for print function in Python3.
print(firstArg)
Brackets are important because print is a function. The print function in Python has some parameters which make it even more powerful.

NameError when doing a basic input [duplicate]

This question already has answers here:
error in python d not defined. [duplicate]
(3 answers)
Closed 8 years ago.
I have a super basic question, but I'm just starting to learn python. My script:
print('What is your name?')
person = input("Enter name: ")
print("Hello ", person)
is returning an error: NameError: name 'Bob' is not defined.
I have basically just copied and pasted what was from the tutorial at this point, but it still doesn't work unless I put the name in quotation marks. What am I doing wrong?
Your code should work perfectly fine in Python 3. However, in Py2 it will throw a NameError as there are differences between input() and raw_input(). Essentially, input() in Python 2 is the same as eval(raw_input(""Enter name: ")), meaning that it will attempt to run the inputed code as Python.
In Python 3, raw_input() is no more, and input() operates the way you are expecting it to here: Print a line, accept input, and assign it in string format to a variable.
You simply are not using Python 3. In Python 2, input() works differently; see this excerpt from the docs.
Equivalent to eval(raw_input(prompt)).
So, when you type Bob without quotation marks, you are basically saying eval(Bob). With quotes it is eval("Bob"). raw_input() does not exist in Python 3, so it will not be defined if you are running with Python 3.
Make sure that you are running your file with Python 3:
Type which python and which python3 to make sure you have both.
Run python and python3 and see which versions you are running when the python shell opens.
You can check the version of Python which is running your code by doing import sys; print(sys.version).
Run your code via python3 yourfile.py.
As J.F. Sebastian notes, you should add #!/usr/bin/env python3 to the very first line of your file. Chances are, you either wrote #! /usr/bin/env python which on OS X is 2.7.x (I think it's 2.7.2, and I think python is a symlink to the python27 binary) and are running your file just by typing its name, or you are running it through python.

Python: invalid syntax in function print()

I am using Python 2.7.
When I try to print a simple string to a file, I get the following error:
Syntax error: invalid tuple
Syntax error while detecting tuple
minimal example:
fly = open('workfile', 'w')
print('a', file=fly)
writing to the same file via fly.write('a') works just fine.
You are using the Python 3 syntax in Python 2.
In Python 2, it's like this:
print >> fly, 'a'
However, a better idea is to do this:
from __future__ import print_function
Which will enable the Python 3 syntax if you are using Python 2.6 or 2.7.
See also: http://docs.python.org/2/library/functions.html#print
Check the documentation
Note This function is not normally available as a built-in since the name print is recognized as the print statement. To disable the statement and use the print() function, use this future statement at the top of your module:
from future import print_function

Categories