小编Adi*_*n D的帖子

迭代数据类键和值的更好方法是什么?

我有两个数据类,Route并且Factors. Route包含一个值和三个副本Factors

Route不知道Factors包含多少个变量。我想获取这些变量的名称,然后获取Factors.

这是我目前拥有的:

@dataclass
class Factors:
    do: bool  # does it do the route
    hub: int # how many of the locations are hubs

    def __init__(self, do_init):
        self.do = do_init
        self.hub = 0 # will add later 

    def __str__(self):
        return "%s" % self.do


@dataclass
class Route:
    route: tuple
    skyteam: Factors
    star: Factors
    oneworld: Factors

    def __init__(self, route):
        self.route = route.get('route')
        # this could probably be done with …
Run Code Online (Sandbox Code Playgroud)

python python-dataclasses

40
推荐指数
4
解决办法
5万
查看次数

标签 统计

python ×1

python-dataclasses ×1