Python 3.12: Unpacking Three Exciting New Features
Before we jump into this week's article, let me tell you about Graphext.
If you're a business analyst or data scientist in need of a powerful data exploration and predictive modeling platform, look no further than Graphext. Its intuitive visualizations and cross-filtering capabilities enable you to uncover hidden patterns and correlations in your data faster and more efficiently than relying solely on Python.
Here are some of the highlights of Graphext:
Exploratory Data Analysis Made Easy: With Graphext, you can go beyond spreadsheets and dashboards to visualize insights and build predictive models effortlessly.
Visualize and Discover: Uncover hidden patterns and correlations in your data with Graphext's intuitive visualizations and cross-filtering capabilities faster than can be done with Python
Seamless Data Integration: Connect Graphext to any modern data warehouse or use simple data sources like Excel, CSV, or Google Sheets, ensuring compatibility with your existing data infrastructure.
Advanced Analytical Techniques: Utilize advanced techniques like unsupervised clustering, topic modeling, and image embeddings to gain deeper insights from your data.
Collaborate and Share: Graphext offers a collaborative environment where you can work with your team, share visualizations, and discuss findings, fostering teamwork and knowledge sharing.
Don't just take my word for it, check it here: graphext.com
If you want to sponsor of this newsletter, please reach out to?[email protected]
Introduction
When it comes to Python, It has become a tradition to release a new version every year, just before the end of the year. This year is no exception; a new version of Python will be released in October of this year. Python continues to demonstrate its commitment to providing a more user-friendly and efficient programming experience, making every new release an exciting update for developers to look forward to.?Even though the new version is not officially out, you can download the pre-release testing version from the official Python website.?In this article, we are going to explore some of the new features of Python 3.12 that you should be excited about.
1. Improved F-strings Syntax
In Python, an f-string is simply a literal string, prefixed with 'f' or 'F', that contains expressions inside braces. In f-strings, the expressions are replaced with their values. F-strings are widely used for string formatting due to their simplicity, readability, and efficiency. They allow developers to embed expressions directly inside strings, making it easy to create dynamic and readable strings with minimal syntax. Here is an example of an f-string:
In the code above, the expressions {name} and {language} are replaced by the values of the "name" and "language" variables. Here are some of the changes that will come with Python 3:12.
In Python 3.11, f-strings had some restrictions that will be removed in Python 3.12. For example, in Python 3.11, it is not possible to reuse the same quote (single or double) that is used to enclose the f-string inside the expression. In the example below, we have the same quote (double quotes) inside the expression as the f-string. This generates a SyntaxError:
This restriction will be removed in Python 3:12. We will be able to use the same quotes inside the expression, and it will not generate a SyntaxError. When we run this code using the new Python version, it works just fine. See below:
In Python 3.11 and earlier versions, we cannot use backslashes in expressions. This would generate a SyntaxError. See below:
This restriction has now been lifted. Backslashes are now valid in Python 3.12:
领英推荐
In Python 3.11, it is impossible to have an f-string expression that spans multiple lines and includes comments. If we try, we generate a SyntaxError. See an example below:
Multiline expressions and comments are now possible with Python 3.12. If we run this code in Python 3.12, here is what we get:
You can see that in this code above, not only do we have an expression that spans multiple lines, but we have also used an escape sequence and comments. This and more are what you will be able to do with f-strings, when the new Python version is released.
2. Improved Error Suggestion Messages
The upcoming Python 3.12 version is set to introduce significant improvements in error suggestions for all types of errors, including syntax, name, and import errors. One notable area of improvement is in handling NameErrors related to class attributes.
In Python 3.11, when a NameError occurs, the interpreter attempts to suggest variable names of built-ins and names available in the current scope. See the example below:
In the code above, In response to the NameError, Python 3.11 is suggesting the name of a built-in function "sum". This is not very helpful when it comes to rectifying that error.
However, Python 3.12 will take this a step further by providing more accurate and helpful suggestions for NameErrors related to class attributes. When a?NameError is raised?in a method, and the instance has an attribute with the same name as?NameError, then it’ll suggest that you include?self.NAME?when referencing the name,?instead of the closest match in the method scope as we saw in the previous example.
You can see above that Python 3.12 is suggesting that we prepend "self" to num because "num" is the same name as the instance attribute defined in the class.
Overall, this improvement in error suggestions is a valuable enhancement to Python, as it will help developers quickly identify and fix errors in their code, leading to improved code quality and development efficiency.
3. The New Override Decorator for Static Typing
In Python 3.11, when a child class overrides a method from its parent class, the child class provides its own implementation of the method, and the parent class method is effectively hidden. If changes are made to the method in the parent class, the child class will not raise any errors or warnings to indicate that the method in the child class might need to be updated as well.
This behavior can lead to bugs and unexpected results in the code, especially when making changes to a base class that is widely used across different child classes.
In the code above the "Child" class is supposed to inherit two methods from the "Parent" class (first_method and second_method), but you must have noticed that the second method has been misspelled as second_methods. This bug will be difficult to catch because mostly because the code will work just fine.
To address such issues, Python 3.12 introduces the @override decorator. The @override decorator indicates to type checkers that the method is intended to override a method in the parent class. This allows type checkers to catch mistakes where a method that is intended to override a method in the parent class does not actually do so due to misspelled method names for instance. Here is a modified code below that includes the @override decorators.
By using the @override decorator, Python will raise an error if the first_method and second_methods in the Child class are not actually overriding methods from the Parent class. This helps to prevent potential bugs and improves code quality by ensuring that method overrides are correctly implemented.
Conclusion
These are just a few of the many new features that will come with Python 3.12. I hope that you are excited to experiment with the new features. Thank you for reading this article. Please?share?this?story?and?subscribe?to this newsletter if you are not yet a subscriber. You can also follow me on?LinkedIn.
TECHNICAL LEAD
11 个月I learned this @override?concept newly here. Thank you
Jr. Data Scientist | Machine Learning | SQL | Python
11 个月Thanks for the informative article ??
Data Scientist/Data Analyst/Machine Learning Engineer/ Data Storyteller/SQL/Tech./"Unleashing the Power of Data: Data Scientist and AI Alchemist, Aiming to Revolutionize the Future of Analytics"
1 年Eagerly waiting for the release.
Interesting share ??
We were thrilled to see our company mentioned in your newsletter! Thank you for the shoutout - it means a lot to us. Your newsletter is always a great source of insights and updates in the data industry, and we appreciate the value you bring to the community. Keep up the great work!