---
title: "In Python how to get a reference to the class in a static method?"  
description: "In Python how to get a reference to the class in a static method?"  
author: "Utpal Vishwas"  
published: 2023-04-11  
updated: 2023-04-24  
canonical: https://www.mindstick.com/forum/157750/in-python-how-to-get-a-reference-to-the-class-in-a-static-method  
category: "python"  
tags: ["python", "python 2"]  
reading_time: 2 minutes  

---

# In Python how to get a reference to the class in a static method?

In [Python](https://www.mindstick.com/articles/75378/simple-yet-useful-tips-when-using-python) how to get a [reference](https://www.mindstick.com/forum/774/reference-what-does-this-error-mean-in-php) to the [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) in a [static method](https://www.mindstick.com/blog/440/creating-static-methods-in-c-sharp)?

## Replies

### Reply by Aryan Kumar

In Python, you can get a reference to the class in a static method by using the **cls** parameter. The **cls** parameter is the conventional name used in Python to represent the class in a method, just as **self** is the conventional name used to represent the instance of the class.

Here is an example:

```python
class MyClass:
   @staticmethod
   def my_static_method(cls, arg1, arg2):
       print("Class:", cls)
       print("Arguments:", arg1, arg2)
MyClass.my_static_method("hello", "world")
```

In this example, we define a class **MyClass** with a static method **my_static_method**. Inside the method, we print the class and the arguments that are passed to the method.

To get a reference to the class in the static method, we use the **cls** parameter. When we call the static method using the class name (**MyClass.my_static_method**), Python automatically passes a reference to the class as the first argument, which is then assigned to the **cls** parameter.

### Reply by Krishnapriya Rajeev

In Python, you can get a reference to the class in a [static](https://www.mindstick.com/articles/12098/the-static-keyword-the-static-methods) [method](https://www.mindstick.com/forum/166/webservice-method) by using the *cls* keyword. The *cls* keyword is used to refer to the class itself, rather than an instance of the class.

Here is an example:

```plaintext
class MyClass:
   @staticmethod
   def my_static_method(cls):
       print(cls)

MyClass.my_static_method(MyClass)

# Output = <class '__main__.MyClass'>
```

In the above example, the my_static_method is a static method and takes a cls argument that refers to the class itself. You can then call this method on the class.


---

Original Source: https://www.mindstick.com/forum/157750/in-python-how-to-get-a-reference-to-the-class-in-a-static-method

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
