Skip to main content

A direct call may doesn't work, but you can use getattrgetattr and hasattrhasattr to make it work.

class A:
    A_class_var = "I am an A class variable"

    def __init__(self):
        self.A_inst_var = "I am an A instance variable"

    @staticmethod  # If it's a classmethod, use the staticmethod to decorate the method
    def GetClassVar(cls):
        return cls.A_class_var

    def GetInstVar(self):
        return self.A_inst_var


class B:
    def __init__(self, cls):
        if hasattr(cls, "GetClassVar"):
            func = getattr(cls, "GetClassVar")
            func(cls)
        b = cls()
        b.GetInstVar()

if __name__ == '__main__':
    b  = B(A)

A direct call may doesn't work, but you can use getattr and hasattr to make it work.

class A:
    A_class_var = "I am an A class variable"

    def __init__(self):
        self.A_inst_var = "I am an A instance variable"

    @staticmethod  # If it's a classmethod, use the staticmethod to decorate the method
    def GetClassVar(cls):
        return cls.A_class_var

    def GetInstVar(self):
        return self.A_inst_var


class B:
    def __init__(self, cls):
        if hasattr(cls, "GetClassVar"):
            func = getattr(cls, "GetClassVar")
            func(cls)
        b = cls()
        b.GetInstVar()

if __name__ == '__main__':
    b  = B(A)

A direct call may doesn't work, but you can use getattr and hasattr to make it work.

class A:
    A_class_var = "I am an A class variable"

    def __init__(self):
        self.A_inst_var = "I am an A instance variable"

    @staticmethod  # If it's a classmethod, use the staticmethod to decorate the method
    def GetClassVar(cls):
        return cls.A_class_var

    def GetInstVar(self):
        return self.A_inst_var


class B:
    def __init__(self, cls):
        if hasattr(cls, "GetClassVar"):
            func = getattr(cls, "GetClassVar")
            func(cls)
        b = cls()
        b.GetInstVar()

if __name__ == '__main__':
    b  = B(A)
Source Link
Cui Heng
  • 1.3k
  • 8
  • 10

A direct call may doesn't work, but you can use getattr and hasattr to make it work.

class A:
    A_class_var = "I am an A class variable"

    def __init__(self):
        self.A_inst_var = "I am an A instance variable"

    @staticmethod  # If it's a classmethod, use the staticmethod to decorate the method
    def GetClassVar(cls):
        return cls.A_class_var

    def GetInstVar(self):
        return self.A_inst_var


class B:
    def __init__(self, cls):
        if hasattr(cls, "GetClassVar"):
            func = getattr(cls, "GetClassVar")
            func(cls)
        b = cls()
        b.GetInstVar()

if __name__ == '__main__':
    b  = B(A)