You can also use assignment expressions within f-strings to create compact and dynamic strings:
print(f"Square has area of {(area := length**2)} perimeter of {(perimeter := length*4)}")
Positional-only arguments#
In Python 3.8, you can now define positional-only arguments for your own functions. Positional-only arguments can only be assigned by their position in a function’s argument and will throw an error if you try to assign them through keywords. You use the symbol /
in a function’s arguments to denote that all previous arguments should be positional-only.
For example:
>>> def parkingSpace(name, /, space= "Guest Parking"): ... return f"{name}: {space}" ... >>> parkingSpace("Ryan") # `Ryan: Guest Parking' >>> parkingSpace("Jodie", space= "14G") # 'Jodie: 14G' >>> parkingSpace(name="Joe", space="9B") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: parkingSpace() got some positional-only arguments passed as keyword arguments: 'name'
On line 1, we specify that name
must be passed positionally (behind the /
), while space
must be passed with its keyword. On line 4, we successfully call parkingSpace
and positionally assign the value of name
with a default value for space
. On line 7, we successfully call parkingSpace
again, but this time, we assign a new value to space
with its keyword. Finally, on line 10, we call parkingSpace
a third time but assign name
with its keyword rather than positionally and get an error.
Positional-only arguments allow for advanced functionality but are not always needed.
Use positional-only arguments when:
- The argument title is arbitrary such as an int constructor,
int (x="1")
gives no more information than just int("1")
. - The argument title is also used by a built in function, like
self
. Positional-only arguments remove confusion on what is being passed. - You’re porting code from C or will do so in the future. C does not accept keyword arguments so using positional-only arguments will make porting easier.
Syntax warnings#
Python 3.8 adds two new warnings to Python’s list of SyntaxWarnings
. The first is a warning that will tell you when to choose is
over ==
. These are slightly different and easy to mix up. The latter checks for equal values, while is
returns True only they’re the same objects.
>>> # Python 3.8 >>> version = "3.8" >>> version is "3.8" <stdin>:1: SyntaxWarning: "is" with a literal. Did you mean "=="? # returns False >>> version == "3.8" # returns True
The other warning is a revised SyntaxWarning when you miss a comma in a list. In previous versions, missed commas in a list of tuples would result in an error, stating “tuples are not callable”. This confused Python users, as most of the time, they were just missing a comma.
Now, Python 3.8 correctly identifies the error:
>>> [ ... (1, 3) ... (2, 4) ... ] <stdin>:2: SyntaxWarning:'tuple' object is not callable; perhaps you missed a comma? Traceback (most recent call last): File "<stdin>", line 2, in <module> TypeError: 'tuple' object is not callable
Python 3.8 cheatsheet#