Python Forum

Full Version: How to access parent object attribute
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
There are class A and class B.
Class B is inherited from A:
class B(A):
How to access attribute attr_a1 of class A from the object b of class B ?
Thanks.
Not sure if this is what you are after, if not you need to give a better example of what you are after.
class ClassA:
    attr_a1 = "attr_a1"

class ClassB(ClassA):
    pass

object_b = ClassB()

print(object_b.attr_a1)
Output:
attr_a1
Parent/Child is usually reserved for object relationships, not class relationships. Use super/sub when describing a class inheritance relationship. In this example class B inherits print() from class A. B is a subclass of A and A is a superclass of B. Sounds a little odd, really. You would think the superclass would be more "super", but it it is the subclass that has greater abilities. A subclass can do everything its superclass can do, and more.
class A:
    def print(self):
        print("I am an", self.__class__.__name__)

class B(A):
    pass

a = A()
b = B()
a.print()
b.print()
Output:
I am an A I am an B
A subclass can override inherited attributes. In this example B replaces A.print() with a new method B.print()
class A:
    def print(self):
        print("I am an", self.__class__.__name__, 'I got it from A')

class B(A):
    def print(self):
        print("I am an", self.__class__.__name__, 'and this is not inherited')

a = A()
b = B()
a.print()
b.print()
Output:
I am an A I got it from A I am an B and this is not inherited
Be forewarned, most attributes in Python are Object attributes, not Class attributes and object attributes are not inherited at all.
class A:
    def __init__(self):
        self.value = 'I am an A'
        self.value2 = 'really'

    def print(self):
        print(self.value)
        print(self.value2)

class B(A):
    def __init__(self):
        self.value = 'I am a B'

a = A()
b = B()
a.print()
b.print()
Output:
I am an A really I am a B Traceback (most recent call last): File "...", line 17, in <module> b.print() File "...", line 8, in print print(self.value2) AttributeError: 'B' object has no attribute 'value2'
value and value2 are instance attributes. They can have different values for every instance of the object. As the error message shows, instance variables are not inherited. Just because class A.__init__() makes an instance variable doesn't mean the same instance variable is made for instances of class B. To guarantee that instances of B will have the same attributes as instances of class B, class B's __init__() should call class A's __init__().
class A:
    def __init__(self):
        self.value = 'I am an A'
        self.value2 = 'really'

    def print(self):
        print(self.value)
        print(self.value2)

class B(A):
    def __init__(self):
        super().__init__()
        self.value = 'I am a B'

a = A()
b = B()
a.print()
b.print()
Output:
I am an A really I am a B really