从导入的文件继承时获取追溯

时间:2018-04-27 11:22:51

标签: python python-2.7 multiple-inheritance

@ first.py

class first():

   def fun1():
       print 'first one'

@ second.py

class second():

    def fun2():
        print 'second one'

@ third.py

import first

import second

class third (first, second):

    def fun3():
        f= first()
        s= second()
        f.fun1()
        s.fun2()
        print 'third one'

在运行third.py时,我得到了追溯

Traceback (most recent call last):
  File "C:\Python27\third.py", line 4, in <module>
    class third (first, second):
TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)

1 个答案:

答案 0 :(得分:0)

在您的代码中导入文件但没有导入类

@ first.py

class First(object):

   def fun1(self):
       print 'first one'

@ second.py

class Second(object):

    def fun2(self):
        print 'second one'

@ third.py

import first

import second

class Third(object):

    def fun3(self):
        f= first.First()
        s= second.Second()
        f.fun1()
        s.fun2()
        print 'third one'


Third().fun3()
#output:
first one
second one
third one

另外,您可以从文件中调用第一和第二类的另一种方法如下:

@ third.py

from first import First
from second import Second

class Third(object):
    def fun3(self):
        f= First()
        s= Second()
        f.fun1()
        s.fun2()
        print 'third one'


Third().fun3()