nonlocal
: allow modification of outer function variable from inner function
*args, **kwargs
in function arguments
*args
: collect positional arguments as a list**kwargs
: collect arguments as a dictionaryEllipsis
with value ...
or Ellipsis
NoneType
with value None
:=
): assignment that is also an expressionsys.setrecursionlimit()
ᵈl
is empty: "empty" if not l else "non-empty"
l1 + l2
functools.singledespatch
: used to have something like function overloading of C++
functools.register
sort()
vs sorted()
list.sort()
:
sorted()
:
https://docs.python.org/3/howto/sorting.html
def foo(pos_only_args, /, pos-or-kw-args, *, kw-only-args):
body
*args
and **kwargs
*args
: positional arguments*kwargs
: keyword argumentsInvolves a mini-language.
{
and }
should be {{
and }}
respectively.—
Print variable name and value (useful for debugging):
>>> a=3
>>> f"{a=}"
'a=3'
>>> f"{a}"
'3'
—
Sign (only for numbers):
Syntax | Sign |
---|---|
+ |
Print sign for all numbers |
- |
Pring sign only for negative numbers |
Space | Print sign for negative and space for positive |
z |
Only for floats. -0.0 will become 0.0 ?? |
z
option is only from v3.11.
More: #
, ,
, _
—
Alignemnt:
Syntax | Align | Comment |
---|---|---|
< |
Left | |
> |
Right | |
^ |
Center | |
= |
Padding before number but after sign | numbers only |
>>> s="hello"
>>> f"{s:^10}"
' hello '
>>> f"{s:10}"
'hello '
>>> f"{s}"
'hello'
>>> f"{s:*^11}"
'***hello***'
>>> f"{s:=10}"
Traceback (most recent call last):"<stdin>", line 1, in <module>
File ValueError: '=' alignment not allowed in string format specifier
—
Conversion
Syntax | Function |
---|---|
!r |
repr() |
!s |
str() |
!a |
ascii() |
—
See:
A type with only a single possible value.
>>> type(Ellipsis)
<class 'ellipsis'>
>>> type(...)
<class 'ellipsis'>
>>> Ellipsis: Ellipsis; print("OK")
OK
>>> Ellipsis: ellipsis
Traceback (most recent call last):"<stdin>", line 1, in <module>
File NameError: name 'ellipsis' is not defined
>>> type(ellipsis)
Traceback (most recent call last):"<stdin>", line 1, in <module>
File NameError: name 'ellipsis' is not defined
I guess ellipsis
is something not meant to be accessed by users.
https://docs.python.org/3/library/re.html
>>> c = re.compile("\w\da")
>>> c.match("a0abbb")
<re.Match object; span=(0, 3), match='a0a'>
.
, ^
, $
, *
, +
, ?
{m}
, {m,}
, {m,n}
, {,m}
[]
, |
, (..)
\NUM
: refer to a group
\b
: ??
\B
: ??
\d
: [0-9]
\D
: negation of \d
\s
: whitespaces ([\t\n\f\v ]
and more)
\S
: negation of \s
\w
: [0-9A-Za-z_]
\W
: negation of \w
\A
: match start of the string ??
\Z
: match end of the string ??
—
*?
, +?
, ??
, {m}+
(non-greedy / minimal version)*+
, ++
, ?+
(possessive quantifiers ??)import enum
class Op(enum.Enum):
= 1
ADD = 2 SUBTRACT
match
& case
)https://peps.python.org/pep-0636/* mypy https://docs.python.org/3/library/typing.html
# type: ignore
: skip type checking for line with this at the endA = TypeVar("A")
Any
numpy support type annotations too: https://numpy.org/doc/stable/reference/typing.html
import numpy.typing as npt
Python debugger part of stdlib: https://docs.python.org/3/library/pdb.html
.
is current line.import pdb
pdb.set_trace()
The builtin breakpoint()
available from 3.7 onwards, does the same thing. https://docs.python.org/3/library/functions.html#breakpoint
https://stackoverflow.com/questions/5169702/how-do-i-list-the-current-line-in-python-pdb
alias ll u;;d;;l