Answer the question
In order to leave comments, you need to log in
How to get variables from child class in parent class?
I must say right away that I really need this, and it doesn’t matter that it contradicts the concept of OOP.
There are two classes: Parent and Child. In the child class, we define various variables (as many as we like). And in the parent class, I need to somehow get a list of these declared variables, but I don’t know how. Maybe somehow through __init__ or __new__ is it possible?
The option to add some functions in the child class is not needed. I need to get these variables in the parent class. What for? Because the parent class - it will be one, and there are many children. And I will not repeat the same additional functions in each of them.
Or maybe there is some other implementation idea? To put it mildly, I need the parent class to collect information about the variables from the child classes - their name and value (these variables are generally instances of other classes).
class Parent:
def __new__(...):
...
def __init__(...):
...
class Child(Parent):
first = 1
second = 2
third = 3
...
Answer the question
In order to leave comments, you need to log in
As indicated in the comments, this is contrary to the concept, by the way, not only OOP. The parent class does not know anything about the child class, unlike the latter.
Here in the latter you can issue such appeals and operate at his level. Accordingly, the parent class cannot and should not do what the child can do (minus the parent), but the child already combines both.
This approach, unidirectional data flow leads to security in the first place. And besides, having a final child class in hand, what's the point of trying to shove something into the parent? If there is some kind of processing logic that contains the data of the child, then this logic should be described in the child, not in the parent.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question