Python Forum
changing much of my Python code
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
changing much of my Python code
#1
416 of my Python files have a line beginning with from __future__. i'm thinking i don't need those lines, anymore. should i remove all those lines from those files?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
Why do you think you don't these those lines anymore? The risk is simply to break the code.
Reply
#3
when will the code break? when using Python2? i don't use Python2, anymore. i don't support Python2, anymore. i don't test my code in Python2, anymore. i still have the Python2 interpreter in case i end up running someone else's code that needs it. i have no plans to delete Python2.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
If this is python 2 code that you want to convert to python 3, use the 2to3 utility. It will remove the unnecessary __future__ imports and probably keep the rest of the code working. Note that __future__ imports still exist in python 3.
Reply
#5
You can add:
from __future__ import annotations
PEP 563

The __future__ module is still used to introduce changes.
https://docs.python.org/3/library/__future__.html
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
(Apr-29-2019, 06:56 AM)Gribouillis Wrote: If this is python 2 code that you want to convert to python 3, use the 2to3 utility. It will remove the unnecessary __future__ imports and probably keep the rest of the code working. Note that __future__ imports still exist in python 3.

i had been writing my code to work in both Python2 and Python3. now that Python2 is less than a year from EOL, i have decided to drop it entirely and only write for Python3. i don't need to use the 2to3 utility because all my code already works in Python3 (and most probably still also works in Python2, but i really don't care about that, anymore.

what do the __future__ imports do in Python3? i thought they did nothing, though i could believe that if a Python4 ever comes out that __future__ could have a new use.

if the 2to3 utility removes the __future__ imports then i suppose it is safe for me to do so. just doing only that should have no effect on running that code under Python3, right? if it runs OK with the __future__ imports under Python3, then it should also run without, right?

(Apr-29-2019, 09:58 AM)DeaD_EyE Wrote: You can add:
from __future__ import annotations
PEP 563

The __future__ module is still used to introduce changes.
https://docs.python.org/3/library/__future__.html

none of my code imports annotations (yet). i can add that import if/when i use annotations, right?

Output:
lt2a/phil /home/phil 20> rls +f|pygrep|whathas annotations lt2a/phil /home/phil 21> rls +f|pygrep|whathas __future__|lc 10306 lt2a/phil /home/phil 22>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
so i have a bunch of python files with
from __future__ import division,generators,print_function,with_statement
in them because it was in the file that is initially copied to the buffer when i start editing a new .py file. since i now no longer do any support/testing of Python2 functionality in my code, i see little reason to leave that in them. i have already made all my code run on Python3. some still also works on Python2. lots of that is because of that one line. some might even work without it. i'm even considering adding
if bytes==str:raise RuntimeError('Python version 2 is no longer supported')
to everything.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020