The Instance Class Static Magic Method in Python

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.

This article is also posted on my blog, feel free to check the latest revision: The Instance Class Static Magic Method in Python

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

timerring

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值