小编Cor*_*zin的帖子

Is there a Python equivalent of TypeScript's Pick type?

TypeScript has a Pick type which can take an existing type and create a new one by picking individual attributes. I would like to have a similar functionality in Python, so for example:

from dataclasses import dataclass
from pick import Pick

@dataclasses.dataclass
class Foo:
    x: int
    y: float
    z: str

Bar = Pick(Foo, ('x', 'y'))

foo = Foo(1, 2.0, '3')
bar = Bar(1, 2.0)
Run Code Online (Sandbox Code Playgroud)

I want this to avoid the problem of partial object population without having to explicitly define …

python typing typescript mypy python-dataclasses

6
推荐指数
0
解决办法
754
查看次数

标签 统计

mypy ×1

python ×1

python-dataclasses ×1

typescript ×1

typing ×1