Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
can';t在Python3中排序多重继承_Python_Python 3.x_Inheritance_Multiple Inheritance - Fatal编程技术网

can';t在Python3中排序多重继承

can';t在Python3中排序多重继承,python,python-3.x,inheritance,multiple-inheritance,Python,Python 3.x,Inheritance,Multiple Inheritance,文件上说: Python supports a form of multiple inheritance as well. A class definition with multiple base classes looks like this: class DerivedClassName(Base1, Base2, Base3): <statement-1> . . . <statement-N> For most pur

文件上说:

Python supports a form of multiple inheritance as well. A class 
definition with multiple base classes looks like this:

class DerivedClassName(Base1, Base2, Base3):
    <statement-1>
    .
    .
    .
    <statement-N>
For most purposes, in the simplest cases, you can think of the search 
for attributes inherited from a parent class as depth-first, left-to-
right, not searching twice in the same class where there is an overlap 
in the hierarchy. Thus, if an attribute is not found in 
DerivedClassName, it is searched for in Base1, then (recursively) in 
the base classes of Base1, and if it was not found there, it was 
searched for in Base2, and so on.
但是,我明白了:

Traceback (most recent call last):
  File "classinherit.py", line 13, in <module>
    Blob.dump()
  File "classinherit.py", line 11, in dump
    print('c_1.1 = ' + str(c1_1))
NameError: name 'c1_1' is not defined
回溯(最近一次呼叫最后一次):
文件“classinherit.py”,第13行,在
Blob.dump()
文件“classinherit.py”,第11行,在转储中
打印('c_1.1='+str(c1_1))
名称错误:未定义名称“c1_1”
文档似乎说Python将首先在类Blob的范围内查找(在本例中是类范围内的)变量,而没有找到它将搜索类Class1和Class2……但这显然没有发生


提供了什么?

如果要访问类变量,必须以某种方式引用该类:

class Class1:
        c1_1 = 1.1

class Class2:
        c2_1 = 2.1

class Blob(Class1, Class2):

    @classmethod
    def dump(cls):
        print('c1_1 = ' + str(cls.c1_1))

Blob.dump()

为了清楚起见,您必须始终引用类,而不仅仅是在继承的情况下

例如,以下情况将不起作用:

class Klass():
    v1 = 1

    def dump():
        print('class var = ' + str(v1))

Klass.dump()
NameError:未定义名称“v1”

同样,您必须引用该类才能使其正常工作:

class Klass():
    v1 = 1

    @classmethod
    def dump(cls):
        print('class var = ' + str(cls.v1))

Klass.dump()

如果不想使用
@classmethod
,还可以执行以下操作:

class Klass():
    v1 = 1

    def dump():
        print('class var = ' + str(Klass.v1))

Klass.dump()

但是请记住,更改类的名称
Klass
也需要更改
Klass.v1

您所做的是,您试图访问类变量和(或)属性,而实际上不知道它属于哪个类或不引用该类。 您可以看到@Mike Scotty的答案,也可以正确地调用类变量,然后您就可以清楚地看到Python中的MRO(方法解析顺序)是如何工作的

class A:
    a = 8

class B:
    b = 9

class C(A, B):
    c = 99
输出

d = C() #After instantiating
print(d.a, d.b, d.c)
>> 8 9 99
# Without instantiating
print(C.a, C.b, C.c)
>> 8 9 99

医生们似乎不是这么说的。如果是这样,为什么还要支持多重继承呢?如果我声明'class Blob():…',我就可以很容易地说“print('c1_1='+str(Class1.c1_1))”。多重继承有什么好处,为什么文档似乎不这么说?那么类继承有什么好处呢?我也可以这样做:声明'class Blob():'然后打印'str(Class1.c1_1')。@classmethod似乎是一种笨拙的方法,它会带来更多的混乱。而且,它不适用于文档。您的问题在于访问类变量,请参阅我的更新答案。在这里提出类方法会让人困惑。您可以同样轻松地使用带有
self
@juanpa.arrivillaga的普通方法,但OP在他的问题中没有创建类实例,因此我没有使用实例方法。实际上,如果只访问类属性而不是实例属性,则创建实例可能会更加混乱,并在更改属性时导致错误的预期。您不需要创建实例来访问父类中的属性。如果需要,您可以执行
print(C.a,C.b,C.C)
,而不必创建实例
d
@Blckknght是的,您是对的,我很快就错过了。编辑我的答案,现在可以了吗?这与多重继承无关,您正在尝试访问未定义的变量<代码>c1_1。你的意思是自我。c1_1Python只会在访问属性时执行基于类的查找。在
dump
d = C() #After instantiating
print(d.a, d.b, d.c)
>> 8 9 99
# Without instantiating
print(C.a, C.b, C.c)
>> 8 9 99