The provided content is an article that explains static methods in Python Object-Oriented Programming, detailing their purpose, syntax, and usage within a class without needing access to instance or class attributes.
Abstract
The article is part of a complete course on Object-Oriented Programming in Python and focuses on static methods. It begins with an introduction to the concept of static methods, emphasizing their role when a method is logically related to a class but does not require access to the class or instance attributes. The author explains the syntax for creating static methods using the @staticmethod decorator and illustrates their practical application with examples, including a Student class scenario. The article also provides a comparison with other method types, such as instance and class methods, and concludes with a summary of the key points about static methods, reinforcing the idea that they are used for utility functions or operations that are self-contained and do not interact with the state of the class or its instances.
Opinions
The author suggests that static methods are useful for defining utility functions that are logically connected to a class but do not need to modify or access the class's or object's state.
It is implied that a clear understanding of static methods can enhance the design of a class by allowing the inclusion of methods that perform operations independent of class or instance data.
The article conveys the opinion that static methods contribute to better code organization by grouping related functionality under a class namespace, even if those functions do not operate on instance or class data.
The author encourages the reader to practice defining and using static methods by providing a concrete example and inviting them to follow along with the course material available on GitHub and other linked resources.
There is an underlying opinion that mastering static methods is an essential step in becoming proficient in Python's object-oriented programming paradigm.
The author emphasizes the importance of proper naming conventions for static methods, suggesting that all letters should be lowercase and underscores used for multiple words.
The article subtly promotes the benefits of Medium membership, suggesting that readers can support the author and gain access to a wealth of educational content by subscribing.
Static Methods: Python OOP Complete Course — Part 6
This article is a part of The Complete Course in Object Oriented Programming in Python which you can find it here.
All resources are available in the “Resources” section below.
This article is also available as a YouTube video here.
Introduction
So far, you have learned how to add methods and attributes on the class level which are class attributes and class methods and on the object level which are instance attributes and instance methods. But what if you want to define a method that does need access neither to the object nor to the class itself?
Let us see in this article. So…
So far, we have learned about the two types of attributes and two types of methods which are:
Instance Attributes
Class Attributes
Instance Methods
Class Methods
This article is focusing on the last type of methods that you will learn about in this course which is
Static Methods
(If you like to remember the Instance Attributes and Instance Methods article, Just click here).(If you like to remember the Class Attributes and Class Methods article, Just click here).
Let us learn about static methods and in which context you have to define a static method.
2. Static Methods
Static Methods are the methods that have a logical connection to the class itself, but they don’t need access to neither instance attributes nor class attributes.
So, why do I have to define a method that doesn’t have any access to any of the attributes?
Simply, sometimes you have methods that are just logically connected to this class even if it doesn’t need this access to any of those attributes.
A common example of static methods is if you want to define some utility functions under one class, and these functions are usually used as helpful functions for the work of other class methods or other classes.
The question now is, how we can create a static method?
3. Static Methods Syntax
To create a static method:
First of all, we define a class and give it a name ClassName.
This method is defined using the keyword def as a normal function in Python and gives the method a name.
After that, you can use as many as you want of positional arguments, and arbitrary arguments.
For static methods, we usethe decorator “@static method”, where this decorator will convert your method from a normal instance method into a static method.
classClassName:
@staticmethoddefstatic_method1(arg1,…, argn, *args, **kwargs):
# do something logically related to the class
@staticmethoddefstatic_method2(arg1,…, argn, *args, **kwargs):
# do something logically related to the class
As usual, don’t forget to use the colon after your function definition to avoid a syntax error.
Note:
The first parameter for this method is not (self: the instance) or (cls: the class) which means that this method doesn’t have any access to the class state “class attributes” ( any information related to the class itself) or to the object state “instance attributes” ( any information related to the object itself)
Remember it does something logically related to the class.
Finally, you can list as many as you wish of static methods and let them do something logically related to the class according to your use case.
Assume we need a method that would return true if the student is passed and return false if the student isn’t passed according to his/herCGPA.
If you think about this method you will figure out that this method does not need any access to either instance attributes or class attributes, it is just logically related to your class.
Now, let us add this method:
Define a new method which is a static method (using the decorator @staticmethod) and its name is_passed.
This method takes the student’s CGPA as an argument, in case this CGPA is greater than 50 it returns True (passed) otherwise it returns False (failed) (refer to the last method in the next example).
Static Methods are the methods that have a logical connection to the class, but they don’t need access to neither instance attributes nor class attributes.
To define a static method, we define it as any normal method in Python, but we use the decorator “@static method”. Refer to Figure 1.
Figure 1: Static methods summary (Image By Author).
Do not forget the colon when you define your functions. Otherwise, you will get a syntax error.
P.S.: A million thanks for your time reading my story. Before you leave let me mention quickly two points:
First, to get my posts in your inbox directly, would you please subscribe here, and you can follow me here.
Second, writers made thousands of $$ on Medium. To get unlimited access to Medium stories and start earning, sign up now for Medium membership,whichonly costs $5 per month. By signing up with this link, you can directly support me at no extra cost to you.