The latest version of Python is now available on Platform.sh! In addition to the deluge of bug fixes and improvements you’d expect with every release, Python 3.8 is packed with new features designed to make your code simpler and the development process easier. To get your existing applications up and running with Python 3.8 right now, just swap out “3.7” with “3.8” in yourDocumentation Index
Fetch the complete documentation index at: https://developer.upsun.com/llms.txt
Use this file to discover all available pages before exploring further.
.platform.app.yaml file:
master, and your upgraded application will be ready for the world to see.
What’s new in Python 3.8?
Sure, it’s nice that you can now use acontinue statement within a finally clause, but let’s get right to the feature we know you want to hear about: the walrus operator.
Assignment expressions
The walrus operator:=, formally known as the “assignment expression,” lets you assign a value to a variable at the same time you’re using the variable. This way, you can avoid double function and object calls. The simplest use case is when you need to test a value in an if statement, but also need to refer back to the value within the if block:
n = len(items) prior to the if statement or calling len(items) a second time in the print statement. Assignment expressions can also be used in while loops where you need to test a condition and also use the tested value inside the loop:
Self-documenting f-strings
Keeping with the theme of making code more readable, Python 3.8 introduces the= operator within f-strings to cause them to self-document. Take this example of wanting to print a selection of variable values:
= to the end of an expression within an f-string, the expression itself—as well as the resulting value—will both be printed:
idx=20, val["status"]='operational'. Now, you don’t have to type out every expression twice!
Positional-only parameters
Two new operators have been introduced to function definition syntax:/ and *. The / operator declares that all parameters defined before it in a function definition must be specified positionally, while the * operator declares that all parameters defined after it must be specified as keyword arguments. Any parameters between these two operators can be passed as positional or keyword arguments. Take this function definition, for example:
def f(a, b=None, /), it can be safely redefined as def f(x, y=None, /) in the future. This is because any call to this function that passes a or b as keyword arguments will raise a TypeError.