python的各版本的不同
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
>>> from pathlib import Path >>> p = Path('/root/test/1.py') >>> p.name '1.py' >>> p.suffix '.py' >>> p.exists() False
>>> from enum import Enum >>> >>> class Test(Enum): ... a = 1 ... b = 2 ... c = 3 ... >>> Test.a <Test.a: 1> >>> Test['a'] # 可按名字索引 <Test.a: 1> >>> list(Test) # 可迭代 [<Test.a: 1>, <Test.b: 2>, <Test.c: 3>]
>>> *range(4) SyntaxError: can't use starred expression here >>> *range(4), 4 (0, 1, 2, 3, 4) >>> [*range(4), 4] [0, 1, 2, 3, 4] >>> {'a': 1, **{'b': 2}} {'a': 1, 'b': 2}
>>> print(*[1], *[2], *{'c': 3}) 1 2 c >>> dict(**{'a': 1}, **{'b': 2}) {'a': 1, 'b': 2}
>>> '%a' % 3.14 '3.14' >>> b'%a' % 3.14 b'3.14'
>>> def fun1(a, b: "字符串或None", c: int = 0)-> int: ... pass ... >>> fun1.__annotations__ {'b': '字符串或None', 'c': <class 'int'>, 'return': <class 'int'>}
async def read_db(db): data = await db.fetch('SELECT ...')
>>> 1_000_111_000 1000111000 >>> '{:_}'.format(1000000) # 格式化字符串时也可输出下划线 '1_000_000'
>>> a = 1 >>> f'a={a}' 'a=1' >>> f'{int(1) + 2}' '3'
class TestBase: subclasses = [] def __init_subclass__(cls, *args, **kwargs): super().__init_subclass__(*args, **kwargs) cls.subclasses.append(cls)
if a := input(): # a = input(); if a: print(a)
>>> (a := 0) + 1 1 >>> a 0
>>> def fun1(a, b, c=0, /, *args, **kwargs): ... pass ... >>> fun1(1, 2, 3) >>> fun1(1, 2)
>>> x = 1 >>> print(f'{x=}') x=1