add a static method to a subclass in python

To add a static method to a subclass in Python, you can use the built-in staticmethod decorator. Here's an example:

main.py
class Parent:
    @staticmethod
    def foo():
        print("Parent's static method")

class Child(Parent):
    @staticmethod
    def bar():
        print("Child's static method")

# Calling static methods on Parent and Child
Parent.foo()  # prints "Parent's static method"
Child.bar()   # prints "Child's static method"
322 chars
14 lines

In the above code, we define a Parent class with a static method foo(). We then create a Child subclass of Parent and add our own static method bar() using the same syntax.

To call a static method on a class or subclass, you can simply use the class name and the static method name, e.g. Parent.foo(). Note that static methods don't require an instance of the class to be created before they can be called.

gistlibby LogSnag