尝试使用 super() 将参数从子类传递给父类时出现问题,Python [重复]

问题描述 投票:0回答:0
`# multiple Inheritance issue solve.


class a(object):
    def __init__(self, a_ratings, a_price):
        print("a -> ", a_ratings, a_price)


class b(object):
    def __init__(self, b_ratings, b_price):
        print("b -> ", b_ratings, b_price)


class c(object):
    def __init__(self, c_ratings, c_price):
        print("c -> ", c_ratings, c_price)


class colab(a, b, c):
    def __init__(self, *args, **kwargs):
        super(a, self).__init__(
            a_ratings=kwargs["a_ratings"], a_price=kwargs["a_price"]
        )
        super(b, self).__init__(
            b_ratings=kwargs["b_ratings"], b_price=kwargs["b_price"]
        )


colab(
    a_ratings="a ratings", a_price="a price", b_ratings="b ratings", b_price="b price"
)`

ERROR DETAILS

当我尝试使用 super() 访问类 a 构造函数时,它指向类 b,

class a(object):
    def __init__(self, a_ratings, a_price):
        print("a -> ", a_ratings, a_price)


class b(object):
    def __init__(self, b_ratings, b_price):
        print("b -> ", b_ratings, b_price)


class c(object):
    def __init__(self, c_ratings, c_price):
        print("c -> ", c_ratings, c_price)

class colab(a, b, c):
    def __init__(self, *args, **kwargs):
        #         super(a, self).__init__(
        #             a_ratings=kwargs["a_ratings"], a_price=kwargs["a_price"]
        #         )
        #         super(b, self).__init__(
        #             b_ratings=kwargs["b_ratings"], b_price=kwargs["b_price"]
        #         )
        super(a, self).__init__("param 1", "param 2")


colab(
    a_ratings="a ratings", a_price="a price", b_ratings="b ratings", b_price="b price"
)

输出

b ->  param 1 param 2
<__main__.colab at 0x2b2252f7940>

所以我有两个问题: -> 如何使用 super() 在多重继承中将参数传递给父类构造函数 -> 为什么 super(a,self) 指的是类 b

我在这里做错了什么?

python multiple-inheritance
© www.soinside.com 2019 - 2024. All rights reserved.