1

Wrote some code and this syntax error keeps on occuring but I can't solve it. Due to wanting this only to print when verbose option is on I have included all code relating to the error line.

from __future__ import print_function
print = print_function
parser.add_argument("-v", "--verbose", action="store_true",help="Help option"
verboseprint = print  if verbose else lambda *a, **k: None 

                if line2_rev:
                    verboseprint "Line2 has now been reversed"

    verboseprint " Line2 has now been reversed"
                                              ^
SyntaxError: invalid syntax

I have attempted using ' ' instead as well as changing the string inside but same error occured. Any ideas?

0

2 Answers 2

3

When you run from __future__ import print_function, print() is a function, not a statement. You cannot use verboseprint as a statement either.

Use it as a function instead:

from __future__ import print_function

parser.add_argument("-v", "--verbose", action="store_true",help="Help option"
verboseprint = print  if verbose else lambda *a, **k: None 

if line2_rev:
    verboseprint("Line2 has now been reversed")

The __future__ import changes the way the compiler works; the print keyword is removed from the language for this specific module, and the built-in print() function which is already present in Python 2 becomes available instead. So instead of:

print "This is printed"

You'd use:

print("This is printed")

but in your code you define a new function that'll work the same.

You don't need to assign print = print_function either in your code.

Sign up to request clarification or add additional context in comments.

Comments

0

When you do import print_function, you do not use something named print_function but you changed to use Python3 syntax instead of print keyword syntax.

When in Python2 traditionally you would say:

print 'something', 'other',

You say with import from __future__ import print_function

print('Something', 'other', end='')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.