Static methods

Static methods do not require object creation.

A static method can always be called, but is part of a class.

Method requires you to create an object: Not the case with static methods.

Related course:
Python Beyond the Basics – Object-Oriented Programming

Example

Class method
A non static method needs an object to be initialized:

The self keyword in the start method refers to the object. Because static methods can be called without object creation, they do not have a self keyword.

Static method
Create a class and add a method. You should explicitly define it’s a static method by adding @staticmethod.

Once you defined the class, you can call the methods directly.

This calls the method without creating an object. Unlike normal class methods, they do not have access to objects variables.

This may be odd, as it seems counter to the concept of object orientated programming.

In most of the computer software you’ll find regular class methods. Static methods are usually used as helpers.

Previous Post
Next Post

Leave a Reply