Keeping your Python environment up-to-date is a vital part of the development process, yet it’s often the most overlooked due to cost, time, and sometimes a general fear of what might break. However, your favorite version of Python has a shelf-life.
With Python 2 support ending on January 1, 2020, you might be thinking to yourself: “Do I really need to upgrade?,” or “Am I really getting better features in Python 3?,” or (worst of all) “Maybe I’ll wait it out a few more months to make the switch.”
Well, we’ve tried to make your internal conundrum easier by compiling a list of the top 10 reasons why you should make the upgrade to Python 3:
AsyncIO is a concurrent module that was introduced in Python 3.4 and is rapidly growing and evolving throughout newer versions of Python. It’s designed to simplify your asynchronous code because there are no callbacks. This library allows you to write concurrent code using the async/await syntax. AsyncIO is garnering a lot of attention, which means there are a lot of tutorials and case studies readily available to show just how easy it is to use. We recommend you check out Hacker Noon’s article on AsyncIO for the Working Python Developer, as well as Real Python’s Complete Walkthrough of AsyncIO to get a better feel of just how powerful this tool really is.
UnicodeDecodeError
nonsense)Since the start of Python 3, all text strings are stored as Unicode. Encoded strings are expressed as binary data via bytes
(see #4 in Tips for Upgrading to Python 3 blog post). This is a drastic improvement from Python 2, where you were allowed to mix unicode
and str
strings, leading to the dreaded UnicodeDecodeError
. Your code is going to be much cleaner and more concise with Python 3 because of these changes, and just think of all the extra time you’ll have on your hands to make other updates!
breakpoint()
built-inIn Python 3.7, there is a built-in function called breakpoint()
that will enter a debugger at the point of call. This is a universal API for invoking a debugger, so there’s no confusion about which debugger to choose from, when to use it, or what to type. This is recommended for use for “debugging on the fly,” and so you can easily turn the debugger off and run your program normally. Journal Dev has a great use case example that shows breakpoint()
in action:
Let’s look at a simple example of breakpoint() function usage. We have a python script python_breakpoint_examples.py
with following code.
x = 10
y = 'Hi'
z = 'Hello'
print(y)
breakpoint()
print(z)
When we execute this script, PDB debugger console opens up.
$python3.7 python_breakpoint_examples.py
Hi
> /Users/pankaj/Documents/PycharmProjects/BasicPython/basic_examples/python_breakpoint_examples.py(8)()
-> print(z)
(Pdb) c
Hello
$
Example via: https://www.journaldev.com/22695/python-breakpoint
In Python 2, the system will default to integer division, which means you have to be very clear when converting integers to floats. If you were trying to divide 1 by 2, you’d end up with 0, due to Python 2 rounding the number per its behavior to “round to nearest, ties away from zero.”
For example, in your Python 2 terminal you might see something like this:
>>> int_one = 1
>>> int_two = 2
>>> int_one / int_two
0
>>> float(int_one) / int_two
0.5
via https://python-3-for-scientists.readthedocs.io/en/latest/python3_features.html
Whereas in Python 3, the default division yields a float, and you can access the integer division using the // operator. You can use the syntax as a more intuitive language (which lessens the barrier of entry for new Pythonistas, too!), all while saving time from not having to add extra zeros or trying to be explicit in converting integers to floats.
In the same example with Python 3, it looks a little something like this:
>>> int_one / int_two
0.5
>>> int_one // int_two
0
via https://python-3-for-scientists.readthedocs.io/en/latest/python3_features.html
Something as small as a few decimals here and there can have a massive impact on your results. By making Python 3 a more natural language when processing integers, your calculations will be more accurate, more consistent, and more repeatable among systems going forward.
Trust us, no one wants to sit and wait for their program to think, especially when using a dictionary. A dictionary maps keys to values, and this is the most common (and useful) way to store data in your Python program. You’re able to access data based on relational meaning, which allows for faster determination, as well as faster programs. The dictionaries in Python 3 are now ordered by design (meaning it’ll save the order in which you input the data). The microbenchmarks show vast improvements on extremely large dictionaries, and speeds are reportedly up to 20% faster (granted, this varies some on usage and actual size). This quick optimization is a significant time-saving update that puts Python 3 even further ahead than Python 2. You can also check out the Speed Center to see how the newest updates are performing.
There’s a laundry list of reasons why Python 3 is more secure, but an important note to point out is that raw_input()
is now gone. Why is that significant, you might wonder? If you’ve been a Pythonista for a while, you might not have ever needed or used the functionality. Beginners, on the other hand, seemingly used this function constantly, which resulted in errors such as:
1. Compared to the name "raw_input", the name "sys.stdin.readline()" is clunky and inelegant.
2. The names "sys" and "stdin" have no meaning for most beginners, who are mainly interested in what the function does, and not where in the package structure it is located. The lack of meaning also makes it difficult to remember: is it "sys.stdin.readline()", or " stdin.sys.readline()"? To a programming novice, there is not any obvious reason to prefer one over the other. In contrast, functions simple and direct names like print, input, and raw_input, and open are easier to remember.
3. The use of "." notation is unmotivated and confusing to many beginners. For example, it may lead some beginners to think "." is a standard character that could be used in any identifier.
4. There is an asymmetry with the print function: why is print not called sys.stdout.print()?
via https://www.python.org/dev/peps/pep-3111/
Granted, raw_input()
has been renamed to just input()
in Python 3. The community voted this to have more universal usage and create a straightforward method for both output and interactive input, as that is one of the core values of an introductory computer language course. As a security precaution, removing raw_input()
(which did not evaluate variables) has lessened likelihood of attackers exploiting variable names and getting a hold of sensitive information. If you like your private information where it belongs - with you - porting to Python 3 is a move we highly recommend you take.
With the use of .sort()
and sorted()
in Python 3, you’re able to customize your program to a micro-level that was not available in Pythons past. Whether you’re alphabetizing customers by customer number, or putting expenses in date order, or anything in between, Python is able to provide the customization you’re looking for. With more and more Data Science and Data Analytics engineers using Python to review their data, there are now numerous sources with how-to documentation and case studies for using.sort()
and sorted()
(and when to use them). We recommend checking out Real Python’s How to Use sorted()
and.sort()
in Python for an in-depth review of ordering in Python.
With the release of Python 3.0 on December 3, 2008, library maintainers have been slowing migrating support and updates to the new 3.x versions of Python rather than continuing support of legacy code written in Python 2. So that means if you’ve been stuck on older versions of Python, you’re absolutely missing out on some fantastic new tools and libraries that were not created for Python 2.
Keep in mind, when you are getting ready to port from Python 2 to Python 3, that there are many libraries that are not forward-compatible and will not be supported in Python 3. Although, this is probably for the best. With legacy software, you have likely been missing updates, feature improvements, or even crucial security patches.
For example, the PyPi (Python Package Index) package manager team is very quick to respond to incidents of malicious libraries and trojan software, but relies on community support. As the community fully shifts away from Python 2, old libraries might fall vulnerable to attackers gaining access to sensitive information or harming your core programs.
Speaking of community support, the Python Software Foundation (the organization behind advancing the Python language) is sunsetting Python2 on January 1, 2020 - meaning there will be no more community support from groups like PyPi (see above #8), and most volunteers or organizations will not assist you in your porting if you wait too long.
It’s worth asking your software vendors what their long-term plan is for support of Python 2, as many will not be willing to support legacy code or create new systems on an outdated version of Python. After pushing back the sunsetting from 2015 to 2020, many did not make the transition, but it is now high time. We highly recommend contacting a provider like Six Feet Up that can help you make the port from Python 2 to Python 3 in an efficient manner before community support is fully non-existent.
For a detailed Python 3 statement on which projects are dropping support, and a timeline of Python version support, please visit: https://python3statement.org/
With so many new updates, features, and patches, it’s becoming harder everyday to write code that is compatible for both Python 2 and Python 3 (see #8). The quicker the community has systems and projects running on Python 3.0 and newer, the fewer updates/patches/workarounds developers have to muster to draw out the life of a program that has exceeded its shelf-life.
At Six Feet Up, we are no longer developing new systems on Python 2, and we would be happy to help you port data from Python 2 to the appropriate version of Python 3 for your project. Let’s talk!