Access class variable value specific to current class through parent method

Viewed 12

I have some classes and a class decorator, simplified version below:

def set_foo(cls):
    cls.foo = 1
    return cls

class Parent:
    @classmethod
    def get_foo(cls):
        return cls.foo

@set_foo
class ChildA(Parent):
    pass

print(ChildA.get_foo()) # A

@set_foo
class ChildB(Parent):
    pass
    print(Parent.get_foo()) # B

Clearly A will succeed since ChildA is passed in as an argument to get_foo, but how would I structure either the function or the call to get something like B to work? It won't work as foo isn't defined in Parent, but it is in ChildB yet I can't access ChildB at the class attribute level.

The idea is that each child class has a class attribute unique to each class which is populated through the decorator. I would like to define one method in the parent class they all inherit to access that value. They would need to be done through class attributes, can't use instance - the class structure is for further use downstream.

0 Answers
Related