Inheritable Tracking Decorator - python

I need a parent class with a decorator defined internally that saves all functions in its class to a list, which is an attribute of the parent class. All children of this class must be able to use the decorator, but storing to a list owned by the specific child.
After numerous attempts at defining such a decorator, I am at a loss for how it would be done. Any help would be greatly appreciated! An example of my preferred usage is shown below.
class Parent:
decorated_functions = []
# insert decorator definition
class ChildOne(Parent):
#decorator
def a(self):
return 'a'
#decorator
def b(self):
return 'b'
class ChildTwo(Parent):
#decorator
def c(self):
return 'c'
class ChildThree(Parent):
#decorator
def d(self):
return 'd'
#decorator
def e(self):
return 'e'
#decorator
def f(self):
return 'f'
ChildOne().decorated_functions
# [<function __main__.ChildOne.a>, <function __main__.ChildOne.b>]
ChildTwo().decorated_functions
# [<function __main__.ChildTwo.c>]
ChildThree().decorated_functions
# [<function __main__.ChildThree.d>, <function __main__.ChildThree.e>, <function __main__.ChildThree.f>]
Update #1
Using Brendan Abel's metaclass, I have tried using the following code.
class Child(Parent):
#decorator
def a(self):
return 'a'
#decorator
def b(self):
return 'b'
print(Child().decorated_functions)
However, Child() does not seem to have an attribute decorated_functions.
AttributeError: type object 'Child' has no attribute 'decorated_functions'
Update #2
The above code now works with Brendan Abel's solution! The issue was a change in syntax for metaclasses Python 3.

You probably won't be able to do this without turning decorated_functions into a property (which allows it to be computed after the class has been created), or using a class decorator or metaclasses. I never thought I'd say this, but a metaclass might be the simplest solution here
def decorator(f):
f.decorated = True
return f
class DecoMeta(type):
def __new__(cls, name, bases, attrs):
decorated_functions = []
for v in attrs.values():
if getattr(v, 'decorated', None):
decorated_functions.append(v)
attrs['decorated_functions'] = decorated_functions
return super(DecoMeta, cls).__new__(cls, name, bases, attrs)
class Parent(object):
__metaclass__ = DecoMeta
Edit:
In Python 3, the metaclass hook is slightly different
class Parent(object, metaclass=DecoMeta):
...

Related

Difference between inheriting from object and object.__class__ for python?

I can see code below
class MetaStrategy(StrategyBase.__class__): pass
I am not sure why not just write code like below
class MetaStrategy(StrategyBase): pass
Definition schematic
class StrategyBase(DataAccessor):
pass
class DataAccessor(LineIterator):
pass
class LineIterator(with_metaclass(MetaLineIterator, LineSeries)):
pass
def with_metaclass(meta, *bases):
class metaclass(meta):
def __new__(cls, name, this_bases, d):
return meta(name, bases, d)
return type.__new__(metaclass, str('temporary_class'), (), {})
If you call self.__class__ from a subclass instance, self.__class__ will use that type of the subclass.
Any class that is expressly specified while using the class will be used naturally.
Take the example below:
class Foo(object):
def create_new(self):
return self.__class__()
def create_new2(self):
return Foo()
class Bar(Foo):
pass
b = Bar()
c = b.create_new()
print type(c) # We got an instance of Bar
d = b.create_new2()
print type(d) # we got an instance of Foo

Calling super() without a base class

I am working through the O Reilly Python Cookbook and have been struggling with the below code. It is to with calling a method on a parent class using super():
class Proxy:
def __init__(self, obj):
self._obj = obj
# Delegate attribute lookup to internal obj
def __getattr__(self, name):
return getattr(self._obj, name)
# Delegate attribute assignment
def __setattr__(self, name, value):
if name.startswith('_'):
super().__setattr__(name, value) # Call original __setattr__
else:
setattr(self._obj, name, value)
if __name__ == '__main__':
class A:
def __init__(self, x):
self.x = x
def spam(self):
print('A.spam')
a = A(42)
p = Proxy(a)
print(p.x)
print(p.spam())
p.x = 37
print('Should be 37:', p.x)
print('Should be 37:', a.x)
The book states:
In this code the implementation of __setatrr__() includes a name
check. If the name starts with an underscore it invokes the original
implementation of __setattr__() using super(). Otherwise, it delegates
to the internally held object self._obj.
I am confused. How does super() work then if there is no explicit base class listed?
What exactly then is super() referring to?
There is always a base class; with none explicitly mentioned, Proxy inherits directly from object.
Each class defines a method-resolution order, determined recursively by its base class(es) and its ancestors. When super() gets called, it resolves to a "proxy" of the next class in the MRO of self, whether or not that class appears in the MRO of the class you are currently defining.
Consider the following classes:
class A:
def foo(self):
print("A.foo")
class B(A):
def foo(self):
super().foo()
print("B.foo")
class C(A):
def foo(self):
super().foo()
print("C.foo")
class D(C):
def foo(self):
super().foo()
print("D.foo")
class E(B,D):
def foo(self):
super().foo()
print("E.foo")
e = E()
The MRO of E is [E, B, D, C, A, object]. When you call e.foo(), you start a chain of calls in MRO order. In particular, the call to super in B.foo does not invoke A.foo, but D.foo, a method in a class B knows nothing about, as D is not an ancestor of B. But both B and D are ancestors of E, which is what matters.

Delegating #classmethods in python

I need a delegated class to delegate a #classmethod. Here's what I've tried:
class Foo(object):
def __init__(self, a):
self.a = a
#classmethod
def from_a(cls, a):
return cls(a)
class Bar(object):
def __init__(self, foo):
elf._foo = foo
def __getattribute__(self, name):
return getattr(self._foo, name)
But, of course this doesn't define how to look up attributes of Foo (not of an instance of Foo), so Bar.from_a(5) will raise an AttributeError. While it is of course possible to do this explicitly by defining a from_a method on Bar or to do this at instantiation by calling Bar(Foo.from_a(5)), I would rather do this implicitly. Ideas?
I started working on what I thought would be a simple approach for this using a metaclass, but it is actually fairly complex. What you should probably be doing here is having Bar inherit from Foo, but I'll show you what I came up with all the same:
import types
import functools
def make_delegating_type(delegatee):
class DelegatingType(type):
def __getattr__(self, name):
obj = getattr(delegatee, name)
if isinstance(obj, (types.FunctionType, types.MethodType)):
#functools.wraps(obj)
def wrapper(*args, **kwargs):
result = obj(*args, **kwargs)
if isinstance(result, delegatee):
return self(result)
return result
return wrapper
return obj
return DelegatingType
class Foo(object):
def __init__(self, a): self.a = a
#classmethod
def from_a(cls, a): return cls(a)
class Bar(object):
__metaclass__ = make_delegating_type(Foo)
def __init__(self, foo): self._foo = foo
def __getattr__(self, name): return getattr(self._foo, name)
Note that in 3.x you would use class Bar(object, metaclass=make_delegating_type(Foo) instead of the __metaclass__ = make_delegating_type(Foo) line at the top of the Bar class body.
Here is how this works. Your current version currently delegates attribute lookups on instances of Bar to an instance of Foo, this uses a metaclass so that attributes lookups on the class Bar are delegated to the class Foo as well. Unfortunately it is not as simple as just using a __getattr__ definition that returns getattr(delegatee, name), because if the attribute your a looking up is a factory function as in your example you need a version of that factory function that will return an instance of your delegating type. So for example Bar.from_a(5) should be the same as Bar(Foo.from_a(5)), and with the naive approach you would just get Foo.from_a(5). That is why there is all the logic detecting if the attribute is a function or method, and creating a wrapper that checks the return type of that function/method.
To reiterate, I do not recommend that you use this code! It is much more complicated then just defining from_a on Bar or having Bar inherit from Foo. But hopefully it will be a learning experience for you, as it was for me.

Get class that defined method inside the method

How can I get the class that defined a method in Python?
For example
class A(object):
def meth(self):
return **get_current_class()**
class B(A):
def meth(self):
do_something
return super(B,self).meth()
>>> b=B()
>>> b.meth() ##that return the class A
Since b.__class__ is always the actual class of b(that is B),and what I want is the class which actual defined the method(that should be A),so self.__class__ is useless.
This is a bit of an odd thing to want to do, and you can't do it without getting extremely hacky. One possible solution is to realize that a method is a member of __dict__ on the class that defines it, so you can ascend the MRO to find the first one that has the method name in that dict:
def get_class_for_method(self, method_name):
for cls in self.__class__.mro():
if method_name in cls.__dict__:
return cls
To return the class that defined the method, the simplest way would be to just do it directly:
class A(object):
def meth(self):
return A
If meth is defined outside the class -- and presumably attached to more than one class -- it may not be feasible to hardcode the class in the body of meth. In that case, perhaps the easiest (only?) way for meth to know the class on which it is defined is to store that information in meth (as an attribute) at the time it is attached to the class.
Instead of attaching the function meth to the class A with
A.meth = meth
I propose using a function setmeth which performs both A.meth = meth and meth.thisclass = A:
def setmeth(cls, name, meth):
setattr(cls, name, meth)
setattr(meth, 'thisclass', cls)
def meth(self):
print(meth.thisclass)
class A(object): pass
setmeth(A, 'meth', meth)
class B(A):
def meth(self):
return super(B, self).meth()
b=B()
b.meth() ##that return the class A
prints
<class '__main__.A'>

name of the class that contains the method code

I'm trying to find the name of the class that contains method code.
In the example underneath I use self.__class__.__name__, but of course this returns the name of the class of which self is an instance and not class that contains the test() method code. b.test() will print 'B' while I would like to get 'A'.
I looked into the inspect module documentation but did not find anything directly useful.
class A:
def __init__(self):
pass
def test(self):
print self.__class__.__name__
class B(A):
def __init__(self):
A.__init__(self)
a = A()
b = B()
a.test()
b.test()
In Python 3.x, you can simply use __class__.__name__. The __class__ name is mildly magic, and not the same thing as the __class__ attribute of self.
In Python 2.x, there is no good way to get at that information. You can use stack inspection to get the code object, then walk the class hierarchy looking for the right method, but it's slow and tedious and will probably break when you don't want it to. You can also use a metaclass or a class decorator to post-process the class in some way, but both of those are rather intrusive approaches. And you can do something really ugly, like accessing self.__nonexistant_attribute, catching the AttributeError and extracting the class name from the mangled name. None of those approaches are really worth it if you just want to avoid typing the name twice; at least forgetting to update the name can be made a little more obvious by doing something like:
class C:
...
def report_name(self):
print C.__name__
inspect.getmro gives you a tuple of the classes where the method might come from, in order. As soon as you find one of them that has the method's name in its dict, you're done:
for c in inspect.getmro(self.__class__):
if 'test' in vars(c): break
return c.__name__
Use __dict__ of class object itself:
class A(object):
def foo(self):
pass
class B(A):
pass
def find_decl_class(cls, method):
if method in cls.__dict__:
return cls
for b in cls.__bases__:
decl = find_decl_class(b, method)
if decl:
return decl
print 'foo' in A.__dict__
print 'foo' in B.__dict__
print find_decl_class(B, 'foo').__name__
Will print True, False, A
You can use (abuse?) private name mangling to accomplish this effect. If you look up an attribute on self that starts with __ from inside a method, python changes the name from __attribute to _classThisMethodWasDefinedIn__attribute.
Just somehow stash the classname you want in mangled-form where the method can see it. As an example, we can define a __new__ method on the base class that does it:
def mangle(cls, attrname):
if not attrname.startswith('__'):
raise ValueError('attrname must start with __')
return '_%s%s' % (cls.__name__, attrname)
class A(object):
def __new__(cls, *args, **kwargs):
obj = object.__new__(cls)
for c in cls.mro():
setattr(obj, mangle(c, '__defn_classname'), c.__name__)
return obj
def __init__(self):
pass
def test(self):
print self.__defn_classname
class B(A):
def __init__(self):
A.__init__(self)
a = A()
b = B()
a.test()
b.test()
which prints:
A
A
You can do
>>> class A(object):
... def __init__(self):
... pass
... def test(self):
... for b in self.__class__.__bases__:
... if hasattr(b, 'test'):
... return b.__name__
... return self.__class__.__name__
...
>>> class B(A):
... def __init__(self):
... A.__init__(self)
...
>>> B().test()
'A'
>>> A().test()
'A'
>>>
Keep in mind that you could simplify it by using __class__.__base__, but if you use multiple inheritance, this version will work better.
It simply checks first on its baseclasses for test. It's not the prettiest, but it works.

Categories