Python 继承特定属性

Python 继承特定属性,python,Python,有没有办法只从类继承特定的属性 比如说, class basic(): def __init__(self, a, b, c): self.a = a self.b = b self.c = c class inher(basic): def __init__(self, d, e, f): self.d = d self.e =e self.f = f def func(

有没有办法只从类继承特定的属性

比如说,

class basic():
    def __init__(self, a, b, c):
        self.a = a
        self.b = b
        self.c = c

class inher(basic):
    def __init__(self, d, e, f):
        self.d = d
        self.e =e
        self.f = f

    def func(self):
        # here use 'a' from basic
我知道我可以从basic继承一切:

class inher(basic):
    def __init__(self, a, b, c, d, e, f):
        basic.__init__(self, a, b, c) #inherit these from basic
        self.d = d
        self.e = e
        self.f = f

是否有一种方法可以只从basic继承参数“a”?

似乎您的
basic
类不够原子化。顶级用例如何工作?如果
inher
子类
basic
basic
需要
a
b
c
但是这些都没有提供给
inher
的构造函数,那么它怎么能得到它们呢?对不起,我忘了初始化。如果你不需要类中的所有东西,你不应该从类继承。如果出现这种情况,那么您的程序架构就不好。将basic分为两个类basic和继承一个类。Basic只应具有
a
Inherit\一个人可以具有Basic Inherit和b,c本身。如果
inher
继承自
Basic
,则它的任何实例也
是instance(inh,Basic)
。这意味着你可以用一个代替另一个。但是,如果她中的
不是一个完全的替代品,那么你就在制造各种潜在的问题(你正在破门而入)。所以总的来说,这是个坏主意。