LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2017, 10:00 AM   #16
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 764
Blog Entries: 3

Rep: Reputation: 366Reputation: 366Reputation: 366Reputation: 366

Quote:
Originally Posted by Sefyir View Post
Code:
#!/usr/bin/env python3                                                          
                                                                                
from __future__ import print_function
...out of curiosity, why do you import print function when running Python 3?
 
Old 08-07-2017, 11:30 AM   #17
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 517

Rep: Reputation: 236Reputation: 236Reputation: 236
Quote:
Originally Posted by HMW View Post
...out of curiosity, why do you import print function when running Python 3?
Because it's safe to do so. It's similar to saying, Turn on the new print_function. If it's already on, do nothing.
I put it in there because my original code used print like this: print(obj, end=''). The end='' is a syntax error in 2.7.
Upon refactoring, I did not need that anymore but neglected to remove the __future__ (no harm though)

Quote:
The features recognized by Python 3.0 are absolute_import, division, generators, unicode_literals, print_function, nested_scopes and with_statement. They are all redundant because they are always enabled, and only kept for backwards compatibility.
...
No feature description will ever be deleted from __future__. Since its introduction in Python 2.1 the following features have found their way into the language using this mechanism:
This way, if someone runs python script.py, they will not get a error. Effectively, there's no harm in writing the below in every script to help get rid of a lot of issues of 2.7 users running 3x code (especially the print and division ones). As long of course, you are writing it for python3. It may break code written for python2.7 since it changes fundamental behavior of python.
Code:
from __future__ import absolute_import, division, generators, unicode_literals, print_function, nested_scopes
In your code, if I run it as is: python2.7 yourscript.py I get the syntax error with this line: print(line, end="").
However, if I prepend from __future__ import print_function it suddenly works in python2.7 and python3!

Some demo code:
Code:
$ python2.7
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print('foo', end='')
  File "<stdin>", line 1
    print('foo', end='')
                    ^
SyntaxError: invalid syntax
>>> from __future__ import print_function
>>> print('foo', end='')
foo>>>
Code:
$ python3.6
Python 3.6.2 (default, Jul 17 2017, 23:14:31) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print('foo', end='')
foo>>> from __future__ import print_function
>>> print('foo', end='')
foo>>>

Last edited by Sefyir; 08-07-2017 at 11:41 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to add integer values & get the sum demet8 Linux - Newbie 5 10-20-2012 01:46 PM
Bash script to compare numbers in a txt file leopard86 Programming 6 09-11-2012 12:10 AM
shell script to find the sum of numbers associated with each character in a string Aagam Linux - Newbie 22 02-07-2012 08:59 AM
how to add numbers in a formatted file via a bash script? zero79 Linux - General 8 12-24-2010 05:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration