Skip to main content

Python 3.9 is here!

 

On October 5th Python 3.9 was officially out of beta and realized to everyone. Here I’m gonna describe some important and cool changes that come with Python 3.9.

Simpler Updating of Dictionaries

Merge (|) and update (|=) operators have been added to the built-in dict class. These complement the existing dict.update and {**d1, **d2} methods of merging dictionaries.





One advantage of using | is that it works on different dictionary-like types and keeps the type through the merge:


You can use a defaultdict when you want to effectively handle missing keys. Note that | preserves the defaultdict, while {**europe, **africa} does not.

Type Hinting Generics in Standard Collections

In type annotations, you can now use built-in collection types such as list and dict as generic types instead of importing the corresponding capitalized types (e.g. List or Dict) from typing. Some other types in the standard library are also now generic, for example queue.Queue.

Example:

zoneinfo

The zoneinfo module brings support for the IANA time zone database to the standard library. It adds zoneinfo.ZoneInfo, a concrete datetime.tzinfo implementation backed by the system’s time zone data.

Example:


As a fall-back source of data for platforms that don’t ship the IANA database, the tzdata module was released as a first-party package – distributed via PyPI and maintained by the CPython core team.

A More Powerful Python Parser

One of the coolest features of Python 3.9 is one that you won’t notice in your daily coding life. A fundamental component of the Python interpreter is the parser. In the latest version, the parser has been reimplemented.

Since its inception, Python has used a basic LL(1) parser to parse source code into parse trees. You can think of an LL(1) parser as one that reads one character at a time and figures out how to interpret the source code without backtracking.

One advantage of using a simple parser is that it’s fairly straightforward to implement and reason about. A disadvantage is that there are hard cases that you need to circumvent with special hacks.

In a series of blog posts, Guido van Rossum—Python’s creator—investigated PEG (parsing expression grammar) parsers. PEG parsers are more powerful than LL(1) parsers and avoid the need for special hacks. As a result of Guido’s research, a PEG parser was implemented in Python 3.9. See PEP 617 for more details.

The goal is for the new PEG parser to produce the same abstract syntax tree (AST) as the old LL(1) parser. The latest version actually ships with both parsers. While the PEG parser is the default, you can run your program using the old parser by using the -X oldparser command-line flag:

$ python -X oldparser script_name.py

Alternatively, you can set the PYTHONOLDPARSER environment variable.

The old parser will be removed in Python 3.10. This will allow for new features without the limitations of an LL(1) grammar. One such feature currently being considered for inclusion in Python 3.10 is structural pattern matching, as described in PEP 622.

Having both parsers available is great for validating the new PEG parser. You can run any code on both parsers and compare it at the AST level. During testing, the whole standard library as well as many popular third-party packages were compiled and compared.

You can also compare the performance of the two parsers. In general, the PEG parser and the LL(1) perform similarly. Over the whole standard library, the PEG parser is slightly faster, but it also uses slightly more memory. In practice, you shouldn’t notice any change in performance, good or bad, when using the new parser.

Should you upgrade?

Well, in my opinion always where it is possible. I am always building my projects on the newest versions of software. It keeps me updated with news and it’s supported the longest.

Comments

Popular posts from this blog

How to build FAQ Chatbot on Dialogflow?

  After Google I/O I’m inspired and excited to try new APIs and learn new stuff from Google. This time I decided to try Dialogflow and build a Flutter Chatbot app that will answer some frequently asked questions about Dialogflow. This time I want to be focused more on Dialogflow rather than Flutter. Firstly, go to  Dialogflow ES console , create a new Agent, specify the agent’s name, choose English as a language and click “Create”. As you created a new agent go to setting and enable beta features and APIs and Save. Now let’s model our Dialogflow agent When you create a new Dialogflow agent, two default intents will be created automatically. The  Default Welcome Intent  is the first flow you get to when you start a conversation with the agent. The  Default Fallback Intent  is the flow you’ll get once the agent can’t understand you or can not match intent with what you just said. Click  Intents > Default Welcome Intent Scroll down to  Responses . Clear all Text Responses. In the defau

Vertex AI – One AI platform, every ML tool you need

  This year on Google I/O (Google’s Developer conference) Google presented a new platform that unites all ML tools. Vertex AI brings together the Google Cloud services for building ML under one, unified UI and API. There are many benefits to using Vertex AI. You can train models without code, with minimal expertise required, and take advantage of AutoML to build models in less time. Also, Vertex AI’s custom model tooling supports advanced ML coding, with nearly 80% fewer lines of code required to train a model with custom libraries than competitive platforms. Google Vertex AI logo You can use Vertex AI to manage the following stages in the ML workflow: Define and upload a dataset. Train an ML model on your data: Train model Evaluate model accuracy Tune hyperparameters (custom training only) Upload and store your model in Vertex AI. Deploy your trained model and get an endpoint for serving predictions. Send prediction requests to your endpoint. Specify a prediction traffic split in your

What the Flutter? ExpansionPanel

  A common pattern in apps is to have a list of items that you can expand to show more details. Sometimes these details don’t justify an entirely separate view, and you just need them to show up inline in the list. For that, check out ExpansionPanel, a widget that when tapped on will expand a panel. Start with the  headerBuilder , which returns what the first line of this panel will be. It takes a context and a boolean, so you can change what it looks like when the panel is open vs closed and returns a widget. Next up is the body, which contains the contents of the opened panel. And finally is the boolean  isExpanded  to indicate whether or not this panel is currently open. But what to do with this flag? Well, ExpansionPanels almost exclusively appear as children of ExpansionPanelLists. Here, we can maintain a list of which panels are open and use  ExpansionPanelList’s  expansionCallback parameter to update them. This callback takes an index of the panel that’s just been tapped and whe