21
loading...
This website collects cookies to deliver better user experience
# Function that accepts either `int` or `float`
# Old:
def func(value: Union[int, float]) -> Union[int, float]:
return value
# New:
def func(value: int | float) -> int | float:
return value
value = 42
print(bin(value))
# '0b101010'
print(value.bit_count())
# 3
~ $ ./python3.10 -m pyperf timeit -q --compare-to=python "str()"
Mean +- std dev: [python] 81.9 ns +- 4.5 ns -> [python3.10] 60.0 ns +- 1.9 ns: 1.36x faster (-27%)
~ $ ./python3.10 -m pyperf timeit -q --compare-to=python "bytes()"
Mean +- std dev: [python] 85.1 ns +- 2.2 ns -> [python3.10] 60.2 ns +- 2.3 ns: 1.41x faster (-29%)
~ $ ./python3.10 -m pyperf timeit -q --compare-to=python "bytearray()"
Mean +- std dev: [python] 93.5 ns +- 2.1 ns -> [python3.10] 73.1 ns +- 1.8 ns: 1.28x faster (-22%)
SyntaxError: '{' was never closed
SyntaxError: invalid syntax. Perhaps you forgot a comma?
SyntaxError: did you forget parentheses around the comprehension target?
SyntaxError: ':' expected after dictionary key
SyntaxError: expected 'except' or 'finally' block
SyntaxError: cannot assign to attribute here. Maybe you meant '==' instead of '='?
SyntaxError: f-string: cannot use starred expression here
def foo():
if some:
x = 2
IndentationError: expected an indented block after 'if' statement in line 2
collections.namedtoplo
AttributeError: module 'collections' has no attribute 'namedtoplo'. Did you mean: namedtuple?
def foo(key):
match key:
case 'python':
# do something with py
case 'c++':
# do something with cpp
case _:
# default
case ['a', 'b']:
pass
match some_seq:
case [2, 4, *others]: # The first two elements are known
print("Yes", *others)
case _:
print("No")
match some_seq:
case (1, two, three, 4):
print("Yes", two, three)
case (y, x) if x == y:
print("number")
case (y, *other) if len(other) == 3:
print(other)
match some_dict:
case {"move": direction, "distance": km}:
move(direction, km)
case {"stop": duration}:
sleep(duration)
match k:
case 1 | 2:
print("number")
case (1, _) | (_, 2):
print("sequence")
To beat depression, try to just quit #programming 🤪
— Vadim Kolobanov (@decodesperato) November 22, 2021