0

I am trying to pass some attributes from a method in a parent class to a method in a child class. And after that I wish to use a decorator that uses that attribute.

Suppose I have two classes and a decorator like these:

def my_decorator(fun):
    inner(*args, **kwargs):
        if fun.some_attr == 'value':
            out = fun(*args, **kwargs)
        else:
            out = None
        return out
     return inner

class A:
    def some_method_in_A(self):
        return 1
    some_method_in_A.some_attr = 'value'

class B(A):
    @my_decorator
    def some_method_in_B(self):
        super(B, self).some_method_in_A()

What I want is to have some_attr set to 'value' in some_method_in_B. This attribute has to be available for my_decorator. Is it possible to do it?

This is something that I would have to do for lots of methods in class B so it would be nice to do it in a compact way.

2
  • I think you can't add attribute to a bound method object - stackoverflow.com/a/8847168/5922757 Commented Jul 9, 2016 at 19:37
  • Are you saying that what I did in class A is not ok? It looks ok to me... the reference did not help me. Do you have a more specific one?Thanks any way, you are probably wright, I am not very experienced. Commented Jul 9, 2016 at 20:47

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.