Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

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

Originally Posted by Sefyir View Post
#!/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
Registered: Mar 2015
Distribution: Linux Mint
Posts: 517

Rep: Reputation: 236Reputation: 236Reputation: 236
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)

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, 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.
from __future__ import absolute_import, division, generators, unicode_literals, print_function, nested_scopes
In your code, if I run it as is: python2.7 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:
$ 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='')
$ 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='')

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


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 > Forums > Non-*NIX Forums > Programming

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

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