我正在测试,当我将一个类的实例子类化时,将生成一个新实例,并具有该类的方法,对不起,请问是什么原理?
这是测试代码:
class Base:
def __init__(self, x=None, y=None, z=None):
print 'base ...'
def base_func(self):
print 'base func ...'
t = Base()
class TestA(t):
pass
print type(t),type(TestA)
t.base_func()
TestA.base_func()
执行结果:
base ...
base ...
<type 'instance'> <type 'instance'>
base func ...
base func ...
错误的示例:
class Base:
def __init__(self, x=None, y=None):
print 'base ...'
def base_func(self):
print 'base func ...'
t = Base()
class TestA(t):
pass
print type(t),type(TestA)
t.base_func()
TestA.base_func()
执行将出现一些错误:
base ...
Traceback (most recent call last):
File "ztest.py", line 11, in <module>
class TestA(t):
TypeError: Error when calling the metaclass bases
__init__() takes at most 3 arguments (4 given)