Static methods in Python example

Static methods are almost similar to normal methods inside a class but the only difference is static methods can be called from out side the class without initializing the object of that class.This means a static method can be called without using an object, it can be called using CLASSNAME.STATIC_METHOD_NAME.

Lets see Static methods example

1) Static method example using @staticmethod

class Currency_convert:

    # create Dollar_Ringgit_convert static method
    @staticmethod
    def Dollar_Ringgit_convert(doll):
        return doll * 4.35

print('Dollar to Ringgit Convert :', Currency_convert.Dollar_Ringgit_convert(150))

2) Static method example using staticmethod()

class Currency_convert:

    # create Dollar_Ringgit_convert static method
    def Dollar_Ringgit_convert(doll):
        return doll * 4.35

Currency_convert.Dollar_Ringgit_convert = staticmethod(Currency_convert.Dollar_Ringgit_convert)

print('Dollar to Ringgit Convert :', Currency_convert.Dollar_Ringgit_convert(150))

static methods in python

My Thought

Your email address will not be published. Required fields are marked *

Our Tool : hike percentage calculator