Why Can't I call base class function for my derived class object? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Why Can't I call base class function for my derived class object? (/thread-16061.html) |
Why Can't I call base class function for my derived class object? - AmirAB - Feb-13-2019 Hi experts! Any idea if I am making an error in my code as I am not able to understand Why Can't I call the the base class function for my derived class object? class My_base_class(): def __init__(self): print("This is __init__function of Base Class") def base_class_func1(self): print("my base class function") def base_class_func2(self): print("my base class function # 2") class My_derived_class(): def __init__(self): My_base_class.__init__(self) print("I am inside my __init__function of my Drived Class") def base_class_func1(self): print("my base class function") def derived_class_func1(self): print("I am inside my derived class function 01") my_inherit=My_derived_class() my_inherit.base_class_func2()# I get error while I call function of base class.Error:'My_derived_class' object has no attribute 'base_class_func2' RE: Why Can't I call base class function for my derived class object? - Larz60+ - Feb-13-2019 class My_base_class(): def __init__(self): print("This is __init__function of Base Class") def base_class_func1(self): print("my base class function") def base_class_func2(self): print("my base class function # 2") class My_derived_class(My_base_class): def __init__(self): My_base_class.__init__(self) print("I am inside my __init__function of my Drived Class") def base_class_func1(self): print("my base class function") def derived_class_func1(self): print("I am inside my derived class function 01") my_inherit=My_derived_class() my_inherit.base_class_func2()# I get error while I call function of base class. RE: Why Can't I call base class function for my derived class object? - snippsat - Feb-13-2019 To fix and update some stuff It not a function when it has self ,then is a method that belong to the class. Start using super() ,and look at PEP-8class BaseClass: def __init__(self): print("This is __init__method of Base Class") def base_method1(self): print("Only work in Base class <overridden> in DerivedClass") def base_method2(self): print("my base class method # 2") class DerivedClass(BaseClass): def __init__(self): super().__init__() print("I am inside my __init__method of my Drived Class") def base_method1(self): print("DerivedClass class method") def derived_method1(self): print("I am inside my derived class method 01") my_inherit = DerivedClass() my_inherit.base_method2()
To take a little about Method overriding ,which can be confusing.See my comment Only work in Base class <overridden> in DerivedClass So using DerivedClass will base_method1 in BaseClass has no functions as is overridden.To show a example where both method has same name,but we as add information about date. This can be called pre-filtering and post-filtering if doing method overriding.from datetime import datetime class BaseClass(): def __init__(self): print("This is __init__method of Base Class") def base_method1(self, message): print(f'base_method1 say {message}') def base_method2(self): print("My base class method # 2") class DerivedClass(BaseClass): def __init__(self): super().__init__() print("I am inside my __init__method of my Drived Class") def base_method1(self, message): date_info = f'at time {datetime.now():%B %d, %Y}' super().base_method1(f'{message} {date_info}') def derived_method1(self): print("I am inside my derived class method 01") my_inherit = DerivedClass() my_inherit.base_method2()Use: >>> obj = BaseClass() This is __init__method of Base Class >>> obj.base_method1('hello') base_method1 say helloNow using same base_method1 from DerivedClass ,it will add info about date.>>> obj = DerivedClass() This is __init__method of Base Class I am inside my __init__method of my Drived Class >>> obj.base_method1('hello') base_method1 say hello at time February 13, 2019 |