具有不同签名的Python中的钻石继承

问题描述 投票:1回答:2

这是设置:

class Player(object):
    def __init__(self, heigth):
        self.heigth = heigth
        print('do not forget that this should happen once!')

class Attacker(Player):
    def __init__(self, heigth, goal_probability):
        super().__init__(heigth)
        self.goal_prob = goal_probability

    def hit(self):
        pass
        # implementation

class Goalie(Player):
    def __init__(self, heigth, save_probability=0.1):
        super().__init__(heigth)
        self.save_prob = save_probability

    def catch(self):
        pass
        # implementation

class UniversalPlayer(Attacker, Goalie):
    pass

up = UniversalPlayer(heigth=1.96, goal_probability=0.6)

这一切都按预期工作:MRO首先选择Attacker,然后选择Goalie。我用UniversalPlayerAttacker签名调用__init__的构造函数,用Goalie的签名调用Player的构造函数,它没问题,因为save_probability有一个默认值,但问题是我无法选择save_probability,除了在实例化up.save_probability之后设置up,我发现非常不优雅。

此外,如果Goalie没有save_probability的默认值,则此代码会引发异常。

有没有办法写UniversalPlayer所以我也可以选择save_probability,或者这里有一些根本问题无法解决?

python multiple-inheritance diamond-problem
2个回答
2
投票

__init__的每个附加参数都需要有一个类来负责将它从super的调用中删除,这样当最终调用object.__init__时,你不会意外地将任何参数传递给它。此外,每个方法都必须接受任意参数并将其传递给下一个可能处理的方法。

# Player will be responsible for height
class Player(object):
    def __init__(self, height, **kwargs):
        super().__init__(**kwargs)  # Player needs to use super too!
        self.height = height
        print('do not forget that this should happen once!')


# Attacker will be responsible for goal_probability
class Attacker(Player):
    def __init__(self, height, goal_probability, **kwargs):
        super().__init__(height, **kwargs)
        self.goal_prob = goal_probability

    def hit(self):
        pass


# Goalie will be responsible for save_probability
class Goalie(Player):
    def __init__(self, height, save_probability=0.1, **kwargs):
        super().__init__(height, **kwargs)
        self.save_prob = save_probability

    def catch(self):
        pass
        # implementation

class UniversalPlayer(Attacker, Goalie):
    pass

# Pass all arguments
# Life is easier if you stick to keyword arguments when using super().__init__
up = UniversalPlayer(height=1.96, goal_probability=0.6, save_probability=0.2)

现在,Attacker.__init__是第一个被召唤的人。它使用goal_probability,然后不将其传递给其他调用。它通过save_probability接受**kwargs并传递给Goalie.__init__最终接收。请注意,Attacker.__init__Goalie.__init__都不必在参数列表中明确包含height;它也可以通过**kwargs接受最终被Player.__init__收到。


0
投票

除了事实我不确定单独的类是否是处理这些的最佳方法,问题是你的构造函数不能处理未知的参数。允许他们使用*args, **kwargs表示法。实际上,所有参数都将传递给每个__init__,并忽略未使用的参数。

class Player(object):
    def __init__(self, *args, **kwargs):
        self.height = kwargs['height']

class Attacker(Player):
    def __init__(self, goal_probability, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.goal_prob = goal_probability

    def hit(self):
        pass
        # implementation

class Goalie(Player):
    def __init__(self, save_probability, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.save_prob = save_probability

    def catch(self):
        pass
        # implementation

class UniversalPlayer(Attacker, Goalie):
    pass

up = UniversalPlayer(height=1.96, goal_probability=0.6, save_probability=0.2)
© www.soinside.com 2019 - 2024. All rights reserved.