0

I'm trying to access the "Table_Name variable" of the Child class to make the "Load_Data" method do different stuff depending on the child who cast the parent Class.
Also, is there method to know the child who summoned the parent class?

class DataBaseClass(obj):
    ..
    def Load_Data():
        if Table_Name?? == Child1.Table_Name:
            load_Child1_Data
        if Table_Name == Child2.Table_Name:
            load_Child2_Data    
    ..

class Child1(DataBaseClass):
    ..
    Table_Name = TableName1
    ..

class Child2(DataBaseClass):
    ..
    Table_Name = TableName2
..

import * 
..
Child1.Load_Data()
Child2.Load_Data()
barshopen
  • 1,190
  • 2
  • 15
  • 28
  • why do you want to do this? Reverse inheritance is unnatural. – Yu Zhang Jul 29 '16 at 04:01
  • [Yu Zhang](http://stackoverflow.com/users/3999025/yu-zhang)Lets say I'm too new in OOP. I saw [THIS](http://stackoverflow.com/questions/27670949/python-parent-class-accessing-class-variable-of-child-class) and I just got confused. – Alfredo Alvarado Jul 29 '16 at 04:31

3 Answers3

1

The correct way to achieve this functionality is polymorphism

You need to override the Load_data method in the child classes

class DataBaseClass(object):
    def Load_Data(self):
        raise NotImplementedError("Subclass must implement abstract method")

class Child1(DataBaseClass):
    def Load_Data(self):
        # implement load_data for Child1
        print('Child1')

class Child2(DataBaseClass):
    def Load_Data(self):
        # implement load_data for Child2
        print('Child2')

Child1 = Child1()
Child2 = Child2()

Child1.Load_Data()
Child2.Load_Data()

Output

Child1
Child2
Community
  • 1
  • 1
Mono
  • 330
  • 6
  • 8
1

I think what you are trying to do is something like this:

class BaseClass(object):

    @classmethod
    def load_data(cls):
        try:
            return some_external_load_function(cls.DATA_FILE_NAME)
        except AttributeError:
            raise NotImplementedError(
                'It seems you forgot to define the DATA_FILE_NAME attribute '
                'on you child class.')

class Child1(BaseClass):
    DATA_FILE_NAME = 'my_one_data_file.data'

class Child2(BaseClass):
    DATA_FILE_NAME = 'my_other_data_file.data'

This pattern is completely acceptable in some cases but it is very hard to judge from your pseudo code example if what you are trying to do is one of those cases.

Dr K
  • 416
  • 2
  • 5
  • EXACTLY!! that would be great to implement!!!, but if i wanted to call "load_data" like this
    Child1.load_data(??) is that posible?
    – Alfredo Alvarado Jul 29 '16 at 04:38
  • I am not sure what you mean by `
    ` and `` here, nor the question marks. However, because I made `load_data` a classmethod, it is possible for you to call the methods directly on the child classes without instantiating them first. E.g. to load the data from `my_one_data_file.data` directly into `my_data`, you can do this: `my_data = Child1.load_data()`
    – Dr K Jul 29 '16 at 04:51
0

Just implement the load_data on child classes. Something like this:

class Child1(Base):
    ...
    def load_data():  
        # CHILD1's OWN LOGIC

class Child2(Base):
   ...
    def load_data():  
        # CHILD2's OWN LOGIC

(Update as per your comment.)

If all of the child classes require different logic then you have to write that logic per child. However, if you only have some exceptions then you can override the method only for those.

But if there are only a couple of known possibilities, then you can define a class attribute and only change that attribute on child objects like so:

class Base(obj):
    custom_attribute = 'default_value' 

    def load_data(self):  
        if self.custom_attribute == SOMETHING:
             pass
        else:
             pass    

class Child1(Base):
    custom_attribute = 'child1_value'
    ...

class Child2(Base):
    custom_attribute = 'child2_value'
    ...
Tiny Instance
  • 2,351
  • 17
  • 21
  • Thanks [Tiny Instance](http://stackoverflow.com/users/3659874/tiny-instance) that's the way I'm going to do it!! I just wanted to know if there was a way to save some code lines, 'cause I'll make a lot of child Classes – Alfredo Alvarado Jul 29 '16 at 04:26