Krypton36
Krypton36

Reputation: 23

How can one locate where an inherited variable comes from in Python?

If you have multiple layers of inheritance and know that a particular variable exists, is there a way to trace back to where the variable originated? Without having to navigate backwards by looking through each file and classes. Possibly calling some sort of function that will do it? Example:

parent.py

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

child.py

from parent import parent
class child(parent):
    pass

grandson.py

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

Try to locate where the findMe variable came from with a function call.

Upvotes: 2

Views: 224

Answers (1)

jsbueno
jsbueno

Reputation: 110271

If the "variable" is an instance variable - , so , if at any point in chain of __init__ methods you do:

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

It is an instance variable from that point on, and cannot, for all effects, be made distinct of any other instance variable. (Unless you put in place a mechanism, like a class with a special __setattr__ method, that will keep track of attributes changing, and introspect back which part of the code set the attribute - see last example on this answer)

Please also note that on your example,

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

findMe is defined as a local variable to that method and does not even exist after __init__ is finished.

Now, if your variable is set as a class attribute somewhere on the inheritance chain:

class parent(object):
    findMe = False

class childone(parent):
    ...

It is possible to find the class where findMe is defined by introspecting each class' __dict__ in the MRO (method resolution order) chain . Of course, there is no way, and no sense, in doing that without introspecting all classes in the MRO chain - except if one keeps track of attributes as defined, like in the example bellow this - but introspecting the MRO itself is a oneliner in Python:

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

Again - it would be possible to have a metaclass to your inheritance chain which would keep track of all defined attributes in the inheritance tree, and use a dictionary to retrieve where each attribute is defined. The same metaclass could also auto-decorate all __init__ (or all methods), and set a special __setitem__ so that it could track instance attributes as they are created, as listed above.

That can be done, is a bit complicated, would be hard to maintain, and probably is a signal you are taking the wrong approach to your problem.

So, the metaclass to record just class attributes could simply be (python3 syntax - define a __metaclass__ attribute on the class body if you are still using Python 2.7):

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"])

Now, if one wants to find which of the superclasses defined an attribute of the currentclass, just a "live" tracking mechanism, wrapping each method in each class can work - it is a lot trickier.

I've made it - even if you won't need this much, this combines both methods - keeping track of class attributes in the class'class definitions and on an instance _definitions dictionary - since in each created instance an arbitrary method might have been the last to set a particular instance attribute: (This is pure Python3, and maybe not that straighforward porting to Python2 due to the "unbound method" that Python2 uses, and is a simple function in 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)

Example Classes for the code above:

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__))

And on the console one will get this result:

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' 

Upvotes: 5

Related Questions