Python Forum
Why Can't I call base class function for my derived class object?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why Can't I call base class function for my derived class object?
#3
To fix and update some stuff Wink
It not a function when it has self,then is a method that belong to the class.
Start using super(),and look at PEP-8
class 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() 
Output:
This is __init__method of Base Class I am inside my __init__method of my Drived Class my base class method # 2

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 hello
Now 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
Reply


Messages In This Thread
RE: Why Can't I call base class function for my derived class object? - by snippsat - Feb-13-2019, 03:04 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [split] Class and methods ebn852_pan 15 636 May-23-2024, 11:57 PM
Last Post: ebn852_pan
  [SOLVED] [listbox] Feed it with dict passed to class? Winfried 3 266 May-13-2024, 05:57 AM
Last Post: Larz60+
  Class and methods Saida2024 2 273 May-13-2024, 04:04 AM
Last Post: deanhystad
  How does this code create a class? Pedroski55 6 652 Apr-21-2024, 06:15 AM
Last Post: Gribouillis
  class definition and problem with a method HerrAyas 2 342 Apr-01-2024, 03:34 PM
Last Post: HerrAyas
  Printing out incidence values for Class Object SquderDragon 3 396 Apr-01-2024, 07:52 AM
Last Post: SquderDragon
  class and runtime akbarza 4 486 Mar-16-2024, 01:32 PM
Last Post: deanhystad
  Operation result class SirDonkey 6 648 Feb-25-2024, 10:53 AM
Last Post: Gribouillis
  The function of double underscore back and front in a class function name? Pedroski55 9 844 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  super() and order of running method in class inheritance akbarza 7 875 Feb-04-2024, 09:35 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020