Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python parent class accessing class variable of child class

I'm currently trying to implement some inheritance in my Python project and have hit a road block. I'm trying to create a baseParentClass that will handle the basic functionality for a lot of child classes. In this specific example I am trying to initialize an instance with a number of attributes (set to 0), stored as a class variable list (called ATTRS) in the Child. I am unsure of how to use this ATTRS in the parent class.

class Parent(object):
    def __init__():
        for attr in ATTRS:
            setattr(self, attr, 0)

class Child(Parent):
    #class variable
    ATTRS = [attr1, attr2, attr3]

    def __init__():
        super(Child, self).__init__()

I could store ATTRS as self.ATTRS in Child, and then use self.ATTRS in Parent successfully, but it seems preferable to me to have them stored as a class variable.

Alternatively I could pass ATTRS as a parameter like so:

class Child(Parent):
    #class variable
    ATTRS = [attr1, attr2, attr3]

    def __init__():
        super(Child, self).__init__(ATTRS)

but I'm wondering whether this somehow defeats the point of using inheritance in the first place?

I'd be grateful for any ideas, hints or feedback whether I'm barking up the wrong tree completely!

Thanks

like image 440
David Basalla Avatar asked Dec 27 '14 19:12

David Basalla


People also ask

Can a parent class access child class variable?

The reference holding the child class object reference will not be able to access the members (functions or variables) of the child class. This is because the parent reference variable can only access fields that are in the parent class.

How do you access the variables of parent class in a child class in Python?

Accessing Parent Class Functions This is really simple, you just have to call the constructor of parent class inside the constructor of child class and then the object of a child class can access the methods and attributes of the parent class.

Can a parent class access child class methods?

It has all of the instance variables. The only unusual aspect is that, within child class method definitions, you can't directly access parent class instance variables. For example, if the parent had a height instance variable, child class method definitions wouldn't be able to access this directly.

Can a parent class inherit from a child class python?

Child or subclasses are classes that will inherit from the parent class. That means that each child class will be able to make use of the methods and variables of the parent class.


2 Answers

You were close. This works:

class Parent(object):
    def __init__(self):
        for attr in self.ATTRS:
            setattr(self, attr, 0)

class Child(Parent):
    #class variable
    ATTRS = ['attr1', 'attr2', 'attr3']

    def __init__(self):
        super(Child, self).__init__()

Here, you set the ATTRS list at the class level, and conveniently access it in self.ATTRS at baseclass's __init__.

like image 68
shx2 Avatar answered Oct 22 '22 04:10

shx2


Adding to the above answer,

Although ATTR is a class variable in the child class, the instance variable of the child class is able to access the class variable ATTR because, Python does the following using namespaces

  1. Checks the namespace of the instance (self.dict) for the ATTR variable.
  2. If the above fails then it checks the namespace of its class. So it is able to get the value of the class variable although it's accessed using the instance variable.
like image 27
user2126456 Avatar answered Oct 22 '22 03:10

user2126456