timerring

The Instance Class Static Magic Method in Python

January 14, 2025 · 2 min read · Page View:
Tutorial
Method | Python

The Instance Class Static Magic Method in Python

If you have any questions, feel free to comment below.

So what is the difference between the Instance method, the Class method and the Static method?

Instance method #

The normal method is defined with self as the first parameter. And it can only be called by the instance.

Class method #

The class method is mainly about the class.

  1. You have to use the @classmethod to sign your type.
  2. So you can easily use the variable inside the class via cls.variable
  3. you cannot call the init method because they are called only the instance is created.
  4. You can call the function through ClassName.method_name(others not recommend)

Static method #

The static method is as a normal function. Only has the name related to the class. So that it cannot access the class variable and instance variable.

You can call the function via ClassName.method_name

Magic method #

And there is a special method called the magic method. The magic method is the method that has double underscores at the beginning and end of the method name. Such as __str__, __init__, etc.

The normal methods need to be called, but the magic method is called automatically when some events happen. So if you want to customize your class, you can override the magic method.

The most common operators, for loops, and class operations are all run on the magic method.

Now I will introduce some common magic methods:

init #

The __init__ method is the constructor of the class. It is used to initialize the instance of the class. And it will be called automatically when the instance is created.

new #

It will also be called automatically when the instance is created. But it is the first method to be called, which is prior to __init__.

del #

The __del__ method is the destructor of the class. It is used to destroy the instance of the class. And it will be called automatically when the instance is destroyed. (But there still exists some problems, if the interpreter is terminated, but the instance is not destroyed, the destructor will not be called.)

Most of the magic methods are not commonly used. So I will not introduce them in detail. For more information, you can refer to this article.

Private method #

Besides, there is another special method called the private method. The private method is the method that has two underscores at the beginning of the method name. Such as __method_name.

The private method is used to hide the method from the outside. So that the method cannot be called by the outside, if you try that, you will only get an AttributeError:‘xxx’ object has no attribute ‘__attribute_name’.

The private method and attribute can be accessed by the instance internally, so you can use the public method to access them indirectly.

In deed, there is no real private method in Python, it just converts the method name to _ClassName__method_name() or _ClassName__attribute_name. You can use the dir() function to see the method and attribute inside the instance.

Related readings


<< prev | The Overview of... Continue strolling Docker 101 | next >>

If you find this blog useful and want to support my blog, need my skill for something, or have a coffee chat with me, feel free to: