The Instance Class Static Magic Method in Python

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

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

目录
相关文章
|
API Python
python 详细理解 import ---- openstack自动import class 到 特定命名空间
python 详细理解 import ---- openstack自动import class 到 特定命名空间
111 0
|
4月前
|
Go C++ Python
Python Tricks: String Conversion(Every Class Needs a ___repr__)
Python Tricks: String Conversion(Every Class Needs a ___repr__)
32 5
|
6月前
|
API 开发工具 网络架构
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
|
6月前
|
API 开发工具 网络架构
【Azure Developer】使用Python SDK去Azure Container Instance服务的Execute命令的疑问解释
Azure 容器实例(Azure Container Instances,简称 ACI)是一个无服务器容器解决方案,允许用户在 Azure 云环境中运行 Docker 容器,而无需设置虚拟机、集群或编排器。 ACI 适用于任何可以在隔离容器中操作的场景,包括事件驱动的应用程序、从容器开发管道快速部署、数据处理和生成作业。
|
6月前
|
Python Windows
【Azure 应用服务】App Service For Windows 环境中部署Python站点后,如何继续访问静态资源文件呢(Serving Static Files)?
【Azure 应用服务】App Service For Windows 环境中部署Python站点后,如何继续访问静态资源文件呢(Serving Static Files)?
|
数据挖掘 Linux Python
Python学习笔记丨函数和类相关基础知识总结和易错点分享,包括def、lambda、class等
Python学习笔记丨函数和类相关基础知识总结和易错点分享,包括def、lambda、class等
|
9月前
|
Python
python 格式化、set类型和class类基础知识练习(下)
python 格式化、set类型和class类基础知识练习(下)
60 0
|
9月前
|
索引 Python
python 格式化、set类型和class类基础知识练习(上)
python 格式化、set类型和class类基础知识练习
141 0
|
9月前
|
Python
Python类(class)中self的理解
Python类(class)中self的理解
140 0
|
9月前
|
存储 设计模式 Python
Python中的类(Class)和对象(Object)
Python中的类(Class)和对象(Object)
125 0

热门文章

最新文章