我已經創建了一個元類,它覆蓋了默認的init函數以添加一個私有變量。蟒蛇 - 元類,基礎構造函數不會被調用
問題是如果我沒有明確地調用它,我自己不會調用基類的函數init。
看看下面
class Base(object):
def __init__(self, x, y):
self._xvar = x
self._yvar = y
print("THIS IS THE CONSTRUCTOR", x, y)
class Derived(Base):
pass
def main():
derived = Derived(11, 20)
的例子這將打印
這是構造11 20
即使派生類中從未稱
super().__init__(x, y)
這是我的元類:
class MetaTemplateContent(type):
def __new__(mcs, name, base, dct):
# This is the original init function
orig_init = dct.get("__init__")
# this variable will hold all the functions that has a decorator
# If the function name is _content_wrapper it will handle static methods as well
decorators = []
for _, value in dct.items():
if isinstance(value, types.FunctionType):
if value.__name__ == "_content_wrapper":
decorators.append(value)
elif isinstance(value, staticmethod):
function = value.__func__
if function.__name__ == "_content_wrapper":
decorators.append(function)
# This is our wrapper init function which will act as a stub
def init_wrapper(self, *args, **kwargs):
if orig_init:
orig_init(self, *args, **kwargs)
# This is the local variable I want to add to each instance
# and fill it with all the functions that has the decorator
self._callbacks = getattr(self, "_callbacks", [])
self._callbacks.extend(decorators)
# replace the original init with our stub
dct["__init__"] = init_wrapper
return type.__new__(mcs, name, base, dct)
如果我要我們的基類重寫此:
class Base(object, metaclass=MetaTemplateContent):
def __init__(self, x, y):
self._xvar = x
self._yvar = y
print("THIS IS THE CONSTRUCTOR", x, y)
class Derived(Base):
pass
def main():
derived = Derived(11, 20)
什麼都不會被打印,因爲基類的構造不會被調用。
添加super()。然而初始化(X,Y)的派生的構造函數就可以了:
class Derived(Base):
def __init__(self, x, y):
super().__init__(x, y)
但這是多餘的,我知道,我已經錯過了一些重要的東西在這裏。 爲什麼不調用基類構造函數?
這是蟒蛇3.5.3
不錯的傢伙,乾杯 – Crippin