python - 如何在 Python 中找到继承变量的来源?

标签 python class variables multiple-inheritance

如果您有多层继承并且知道某个特定变量存在,是否有办法追溯到该变量的起源?无需通过查看每个文件和类来向后导航。可能调用某种函数来完成它? 示例:

父级.py

class parent(object):
    def __init__(self):
        findMe = "Here I am!"

child .py

from parent import parent
class child(parent):
    pass

孙子.py

from child import child
class grandson(child):
    def printVar(self):
        print self.findMe

尝试通过函数调用来定位 findMe 变量的来源。

最佳答案

如果“变量”是一个实例变量 - ,那么,如果在 __init__ 方法链中的任何一点,您执行以下操作:

def __init__(self):
    self.findMe = "Here I am!"

从那时起它就是一个实例变量,并且对于所有效果都不能与任何其他实例变量区分开来。 (除非您采用一种机制,例如具有特殊 __setattr__ 方法的类,它将跟踪属性更改,并内省(introspection)代码的哪一部分设置了属性 - 请参阅最后一个示例答案)

另请注意,在您的示例中,

class parent(object):
    def __init__(self):
        findMe = "Here I am!"

findMe 被定义为该方法的局部变量,并且在 __init__ 完成后甚至不存在。

现在,如果您的变量在继承链上的某个位置设置为类属性:

class parent(object):
    findMe = False

class childone(parent):
    ...

通过内省(introspection) MRO(方法解析顺序)链中的每个类的 __dict__ ,可以找到定义了 findMe 的类。当然,如果不内省(introspection) MRO 链中的所有类,就没有办法,也没有意义 - 除非有人跟踪定义的属性,就像下面的示例一样 - 但内省(introspection) MRO 本身是一个简单的事情 python :

def __init__(self):
    super().__init__()
    ...
    findme_definer = [cls for cls in self.__class__.__mro__ if "findMe" in cls.__dict__][0]

同样,您的继承链可以有一个元类,它可以跟踪继承树中所有已定义的属性,并使用字典来检索每个属性的定义位置。相同的元类还可以自动装饰所有 __init__ (或所有方法),并设置一个特殊的 __setitem__ ,以便它可以在创建实例属性时跟踪它们,如上面所列。

这是可以做到的,但有点复杂,难以维护,并且可能表明您对问题采取了错误的方法。

因此,仅记录类属性的元类可以简单地是(python3 语法 - 如果您仍在使用 Python 2.7,则在类主体上定义一个 __metaclass__ 属性):

class MetaBase(type):
    definitions = {}
    def __init__(cls, name, bases, dct):
        for attr in dct.keys():
            cls.__class__.definitions[attr] = cls

class parent(metaclass=MetaBase):
    findMe = 5
    def __init__(self):
        print(self.__class__.definitions["findMe"])

现在,如果想找到哪个父类(super class)定义了当前类的属性,只需一种“实时”跟踪机制,将每个方法包装在每个类中就可以工作 - 这要棘手得多。

我已经做到了 - 即使您不需要这么多,这也结合了两种方法 - 跟踪类'class definitions 和实例 _definitions 中的类属性 字典 - 因为在每个创建的实例中,任意方法可能是最后一个设置特定实例属性的方法:(这是纯 Python3,由于 Python2 使用的“未绑定(bind)方法”,可能无法直接移植到 Python2 ,并且是Python3中的一个简单函数)

from threading import current_thread
from functools import wraps
from types import MethodType
from collections import defaultdict

def method_decorator(func, cls):
    @wraps(func)
    def wrapper(self, *args, **kw):
        self.__class__.__class__.current_running_class[current_thread()].append(cls)
        result = MethodType(func, self)(*args, **kw)
        self.__class__.__class__.current_running_class[current_thread()].pop()
        return result
    return wrapper

class MetaBase(type):
    definitions = {}
    current_running_class = defaultdict(list)
    def __init__(cls, name, bases, dct):
        for attrname, attr in dct.items():
            cls.__class__.definitions[attr] = cls
            if callable(attr) and attrname != "__setattr__":
                setattr(cls, attrname, method_decorator(attr, cls))

class Base(object, metaclass=MetaBase):
    def __setattr__(self, attr, value):
        if not hasattr(self, "_definitions"):
            super().__setattr__("_definitions", {})
        self._definitions[attr] = self.__class__.current_running_class[current_thread()][-1]
        return super().__setattr__(attr,value)

上述代码的示例类:

class Parent(Base):
    def __init__(self):
        super().__init__()
        self.findMe = 10

class Child1(Parent):
    def __init__(self):
        super().__init__()
        self.findMe1 = 20

class Child2(Parent):
    def __init__(self):
        super().__init__()
        self.findMe2 = 30

class GrandChild(Child1, Child2):
    def __init__(self):
        super().__init__()
    def findall(self):
        for attr in "findMe findMe1 findMe2".split():
            print("Attr '{}' defined in class '{}' ".format(attr, self._definitions[attr].__name__))

在控制台上将得到以下结果:

In [87]: g = GrandChild()

In [88]: g.findall()
Attr 'findMe' defined in class 'Parent' 
Attr 'findMe1' defined in class 'Child1' 
Attr 'findMe2' defined in class 'Child2' 

关于python - 如何在 Python 中找到继承变量的来源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36562725/

相关文章:

python - 如何在 Selenium 中获取 'nth-of-type'

python - 将 Pandas 列中的字符串转换为 int

C++类成员变量重新赋值

javascript - 为所有以特定字符串开头的变量赋值

python - 在python 3.6中从公钥到公共(public)地址生成比特币 key 对

python - Flask 可插入 View 和需要登录

java - 在 Java 中不使用类创建对象

python - 简单的Python类继承问题

javascript - 如何在webview中的js中调用java中的变量

java - 如何在 Java 中将包含 "1,2,3,4,5,6"这样的数字的值转换为 int 并转换为数组?