Python - polymorphism in instance methods vs polymorphism in static methods

37 Views Asked by At

What is the practical difference between instance methods and static methods, since these two codes return the same results? I understand that static methods do not operate on ‘self’ and do not have direct access to attributes, but how can I verify that instance methods are truly being overridden (are truly polymorphistic), while static methods are not?

class BaseClass:
    def instance_method(self):
        return "Instance method of base class"

    @staticmethod
    def static_method():
        return "Static method of base class"

class DerivedClass(BaseClass):
    def instance_method(self):
        return "Instance method of derived class"

    @staticmethod
    def static_method():
        return "Static method of derived class"

base_object = BaseClass()
derived_object = DerivedClass()

print(base_object.instance_method())  # Displays: "Instance method of base class"
print(derived_object.instance_method())  # Displays: "Instance method of derived class"

print(BaseClass.static_method())  # Displays: "Static method of base class"
print(DerivedClass.static_method())  # Displays: "Static method of derived class"

print(base_object.static_method())  # Displays: "Static method of base class"
print(derived_object.static_method())  # Displays: "Static method of derived class"

How can I check the truly polymorphism of instance methods? Maybe I understand wrong the whole concept of polymorphism?

1

There are 1 best solutions below

2
quamrana On BEST ANSWER

Like any programming language, you just need to write code in that language to determine what its behaviours are:

class BaseClass:
    def instance_method(self):
        return "Instance method of base class"

    @staticmethod
    def static_method():
        return "Static method of base class"
    
    def normal_base_method(self):
        return self.static_method() + " called from normal_base_method"

class DerivedClass(BaseClass):
    def instance_method(self):
        return "Instance method of derived class"

    @staticmethod
    def static_method():
        return "Static method of derived class"

base_object = BaseClass()
derived_object = DerivedClass()

print(base_object.normal_base_method())
print(derived_object.normal_base_method())

Output:

Static method of base class called from normal_base_method
Static method of derived class called from normal_base_method

The above output shows that calling a method defined solely in the base class changes behaviour because the instance is of the derived class.

Calling derived_object.normal_base_method() really calls BaseClass.normal_base_method, but because self refers to derived_object, the static_method is chosen from DerivedClass.