Python 3.10 第二个 alpha 版来了!最新特性值得关注
△点击上方“Python猫”关注 ,回复“1”领取电子书
作者:James Briggs,编译:机器之心
Python3.9 刚刚发布不久,Python3.10 的第二个 alpha 版本也已于 11 月初发布。透过这个版本,我们或许可以一窥 Python 的未来改变。
类型注释扩展
为什么类型注释很重要
新方法和行为
def f(x: *int | float*) -> float:
return x * 3.142
f(1) # pass
f(1.5) # pass
f('str') # linter will show annotation error
MyType = "ClassName" # ClassName is our type annotation
def foo() -> MyType:
...
from typing_extensions import TypeAlias
MyType: TypeAlias = "ClassName"
def foo() -> MyType:
...
OR
MyType: TypeAlias = ClassName # if we have defined ClassName already
def foo() -> MyType:
...
def get_default_model(targeted_task: Dict, framework: Optional[str], task_options: Optional[Any]) -> str:
...
class DefaultArgumentHandler(ArgumentHandler):
...
def handle_kwargs(kwargs: Dict) -> List:
...
def handle_args(args: Sequence[Any]) -> List[str]:
## 例1
x=[1,2,3,4,5,6]
y=[1,2,3,4]
z=zip(x,y)
print(list(z))
## [(1,1),(2,2),(3,3),(4,4)]
## 例2
x=[1,2,3,4,5,6]
y=[1,2,3,4]
z=zip(x,y,strict=True)
print(list(z))
## ValueError: zip() argument 2 is shorter than argument 1
for x in[0,1,2,3,10,11,12,100,101,102]:
print(f"{x}={x.bit_count()}")
## 0=0
## 1=1
## 2=1
## 3=2
## 10=2
## 11=3
...
## 102=4
x={'hello':0,'world':1}
keys=x.keys()
print(keys)
# dict_keys(['hello','world'])
print(keys.mapping)
# {'hello':0,'world':1}
近期热门文章推荐:
评论