- get attribute from a parent class
- Python get attribute parent
- # Table of Contents
- # Accessing parent class attributes in Python
- # Accessing parent class variables
- # Accessing parent instance variables
- # Using super() vs the class’s name
- # Accessing a method that’s defined in the Parent class
- # Get the name of a Parent class in Python
- # Getting the name of a Parent class when you only have access to an instance
get attribute from a parent class
class B:
def __init__(self):
self.var2 = «B’s variable»
# end def
# end class B
# end class A
What I want to do is to be able to get class A’s ‘var’ variable from the
nested class B. For example, I want to be able to do something like:
print «I can see you %s» % a.var
but. I don’t want to make ‘var’ a class variable. I want it to be an
instance variable but still be viewable by the inner class B. Is this
possible? Any suggestions? Thanks
What I want to do is to be able to get class A’s ‘var’ variable from the
nested class B. For example, I want to be able to do something like:
print «I can see you %s» % a.var
but. I don’t want to make ‘var’ a class variable. I want it to be an
instance variable but still be viewable by the inner class B. Is this
possible? Any suggestions? Thanks
There is no way to do this without changing your code slightly, the reason
being that class B is a static definition, and refers to the same object
in every instantiation of A:
To get the effect you want, you must somehow get a reference to an A
object to the definition of the B object. There are two basic ways to do
this:
1) Move the definition of B into A.__init__, so a new class referencing
the A instance is created each time:
class A:
def __init__(aself):
aself.var = «A’s variable»
class B:
def __init__(bself):
bself.var2 = «B’s variable»
bself.parent = self
aself.B = B
2) Allow an instance of A to be passed in the constructor to B:
class A:
def __init__(self):
self.var = «A’s variable»
class B:
def __init__(self,parent):
self.var2 = «B’s variable»
self.parent = parent
Of the two, I prefer the latter, since it is much faster and the code is
cleaner. The only downside is the redundancy of creating B (you have to
call a.B(a) instead of a.B()).
There is probably a way to get the usage of the former with the efficiency
of the latter by using metaclasses, but I don’t know how to do it (mostly
because I don’t like metaclasses very much).
Python get attribute parent
Last updated: Feb 21, 2023
Reading time · 4 min
# Table of Contents
# Accessing parent class attributes in Python
To access parent class attributes in a child class:
- Use the super() method to call the constructor of the parent in the child.
- The __init__() method will set the instance variables.
- Access any of the parent class’s attributes or methods on the self object.
Copied!class Employee(): cls_id = 'emp-cls' def __init__(self, name): self.salary = 100 self.name = name class Developer(Employee): def __init__(self, name): # 👇️ invoke parent __init__() method super().__init__(name) # 👇️ accessing parent instance variable print(self.salary) # 👉️ 100 # 👇️ accessing parent class variable print(self.cls_id) # 👉️ emp-cls d1 = Developer('bobbyhadz') print(d1.salary) # 👉️ 100 print(d1.cls_id) # 👉️ 'emp-cls'
The code snippet shows how to access parent class variables and parent instance variables from a child class.
# Accessing parent class variables
The cls_id attribute is a class variable.
Class variables can be accessed directly on an instance of the child or the child class itself.
Copied!class Employee(): cls_id = 'emp-cls' class Developer(Employee): def __init__(self, name): # 👇️ accessing parent class variable print(self.cls_id) # 👉️ emp-cls d1 = Developer('bobbyhadz') print(d1.cls_id) # 👉️ 'emp-cls' print(Developer.cls_id) # 👉️ 'emp-cls'
# Accessing parent instance variables
To access parent instance variables, call the class’s constructor method to run the code in the parent’s _ _ init _ _ () method.
Copied!class Employee(): def __init__(self, name): self.salary = 100 self.name = name class Developer(Employee): def __init__(self, name): # 👇️ call parent __init__() method super().__init__(name) print(self.salary) # 👉️ 100 d1 = Developer('bobbyhadz') print(d1.salary) # 👉️ 100
The super() method gives us access to the base class without explicitly referring to it.
# Using super() vs the class’s name
We could replace the call to super() with Employee to achieve the same result.
Copied!class Employee(): def __init__(self, name): self.salary = 100 self.name = name class Developer(Employee): def __init__(self, name): Employee.__init__(self, name) print(self.salary) # 👉️ 100 d1 = Developer('bobbyhadz') print(d1.salary) # 👉️ 100
However, super() is more flexible and more commonly used than explicitly referring to the base class.
The call to the parent’s __init__ method runs the method and assigns the salary and name attributes to the instance.
Now we can access the parent class’s salary and name attributes on an instance of the child class.
The classes in the example assume that a name argument is required.
Here is the same example, but without passing any arguments when instantiating the child class.
Copied!class Employee(): def __init__(self): self.salary = 100 class Developer(Employee): def __init__(self): super().__init__() print(self.salary) # 👉️ 100 d1 = Developer() print(d1.salary) # 👉️ 100
Once the code in the parent’s __init__() method runs, the instance gets assigned a salary attribute, which can be accessed on the self object.
# Accessing a method that’s defined in the Parent class
You can use the same approach to access a method defined in the parent class from the child class.
Copied!class Employee(): def __init__(self, name): self.salary = 100 self.name = name def greet(self): print(f'Hello self.name>') class Developer(Employee): def __init__(self, name): super().__init__(name) print(self.salary) # 👉️ 100 self.greet() # 👉️ Hello bobbyhadz d1 = Developer('bobbyhadz') print(d1.salary) # 👉️ 100 d1.greet() # 👉️ Hello bobbyhadz
The parent defines a greet() method which the child instance can access via the self object.
# Get the name of a Parent class in Python
To get the name of a parent class:
- Use the __bases__ attribute on the class to get a tuple of parent classes.
- Use a for loop to iterate over the tuple.
- Use the __name__ attribute on each class to get the name of the parent classes.
Copied!class Person(): pass class Employee(Person): pass class Developer(Employee): pass immediate_parents = Developer.__bases__ print(immediate_parents) # 👉️ (,) for parent in immediate_parents: print(parent.__name__) # 👉️ Employee print(immediate_parents[0].__name__) # 👉️ Employee
We used the class. _ _ bases _ _ attribute to get a tuple of the parent classes of a class.
# Getting the name of a Parent class when you only have access to an instance
If you only have access to an instance of the class, access the __bases__ attribute on the result of calling the type() class with the instance.
Copied!class Person(): pass class Employee(Person): pass class Developer(Employee): pass d1 = Developer() immediate_parents = type(d1).__bases__ print(immediate_parents) # 👉️ (,) for parent in immediate_parents: print(parent.__name__) # 👉️ Employee print(immediate_parents[0].__name__) # 👉️ Employee
The type class returns the type of an object.