python静态方法

Python静态方法 (Python static method)

In this quick post, we will learn how to create and use a Python static method. We will also have a look at what advantages and disadvantages static methods offer as compared to the instance methods. Let’s get started.

在这篇快速文章中,我们将学习如何创建和使用Python静态方法。 我们还将看看静态方法与实例方法相比有哪些优点和缺点。 让我们开始吧。

什么是静态方法? (What is a static method?)

Static methods in Python are extremely similar to python class level methods, the difference being that a static method is bound to a class rather than the objects for that class.

Python中的静态方法与python类级别的方法极为相似,不同之处在于静态方法绑定到一个类而不是该类的对象。

This means that a static method can be called without an object for that class. This also means that static methods cannot modify the state of an object as they are not bound to it. Let’s see how we can create static methods in Python.

这意味着可以在没有该类对象的情况下调用静态方法。 这也意味着静态方法无法修改对象的状态,因为它们没有绑定到对象。 让我们看看如何在Python中创建静态方法。

创建python静态方法 (Creating python static methods)

Python Static methods can be created in two ways. Let’s see each of the ways here:

Python静态方法可以通过两种方式创建。 让我们看看这里的每种方式:

使用staticmethod() (Using staticmethod())

Let’s directly jump to sample code snippet on how to use the staticmethod() approach:

让我们直接跳转到如何使用staticmethod()方法的示例代码片段:

class Calculator:def addNumbers(x, y):return x + y# create addNumbers static method
Calculator.addNumbers = staticmethod(Calculator.addNumbers)print('Product:', Calculator.addNumbers(15, 110))

Note that we called the addNumbers we created without an object. When we run this program, here is the output we will get:

There were no surprises there. This approach is controlled as at each place, it is possible to create a static method out of a class method as well. Let’s see another approach with the same example here.

请注意,我们在没有对象的情况下调用了我们创建的addNumbers。 当我们运行该程序时,将获得以下输出:

那里没有惊喜。 这种方法在每个地方都受到控制,因此也可以从类方法中创建静态方法。 让我们在这里查看带有相同示例的另一种方法。

使用@staticmethod (Using @staticmethod)

This is a more subtle way of creating a Static method as we do not have to rely on a statement definition of a method being a class method and making it static at each place you make it static.

这是创建静态方法的一种更微妙的方法,因为我们不必依赖于方法的语句定义,该方法是类方法,并且在使它变为静态的每个位置都使其成为静态。

Let’s use this annotation in a code snippet:

让我们在代码片段中使用此注释:

class Calculator:# create addNumbers static method@staticmethoddef addNumbers(x, y):return x + yprint('Product:', Calculator.addNumbers(15, 110))

When we run this program, here is the output we will get:

This was actually a much better way to create a static method as the intention of keeping the method static is clear as soon as we create it and mark it with the @staticmethod annotation.

当我们运行该程序时,将获得以下输出:

实际上,这是创建静态方法的一种好得多的方法,因为在我们创建静态方法并使用@staticmethod批注对其进行标记后,就保持了静态方法的意图很明确。

Python静态方法的优点 (Advantages of Python static method)

Static methods have a very clear use-case. When we need some functionality not w.r.t an Object but w.r.t the complete class, we make a method static. This is pretty much advantageous when we need to create Utility methods as they aren’t tied to an object lifecycle usually.

静态方法有一个非常明确的用例。 当我们需要某些功能而不是对象,而需要完整的类时,我们可以使方法静态化。 当我们需要创建Utility方法时,这是非常有利的,因为它们通常与对象生命周期无关。

Finally, note that in a static method, we don’t need the self to be passed as the first argument.

最后,请注意,在静态方法中,我们不需要将self作为第一个参数传递。

API Reference: Python Documentation

API参考: Python文档

翻译自: https://www.journaldev.com/18722/python-static-method

python静态方法

python静态方法_Python静态方法相关推荐

  1. python静态类_python静态方法和类方法

    0x00 静态方法 定义方式:使用@staticmethod装饰器 参数不用self 访问特性:不能引用或访问类的实例属性(类还没实例化呢,哪来的实例属性) 可以直接访问类属性(通过类.类变量来访问类 ...

  2. python 类方法 静态方法_Python静态方法和类方法

    方法就是一个函数,它作为一个类属性而存在,可以使用如下方式进行声明.访问一个函数: class Student(object): def __init__(self,sex): self.sex = ...

  3. python静态变量和静态方法_python的静态成员变量、实例成员变量、静态方法、类方法、实例方法...

    标签: 静态成员变量(类变量)和普通成员变量(实例变量) 静态成员变量只能通过类名.变量名获得,实例成员变量,通过该实例引用.变量名获得. 在实例对静态成员变量赋值时,实例 python是动态类型的语 ...

  4. python中的静态方法如何调用_关于Python中如何使用静态、类、抽象方法的权威指南(译)...

    对于Python中静态.类.抽象方法的使用,我是一直很迷糊的.最近看到一篇技术文章对这方面解释的很好,在此翻译一下,加深印象,也为有需要的同学提供一个方便. Python中方法是如何工作的: 方法即函 ...

  5. python 实例方法、静态方法、类方法应用场景

    python 实例方法.静态方法.类方法应用场景 百雨于 2019-01-16 09:13:40 发布3201 收藏 20 分类专栏: python ​python专栏收录该内容 77 篇文章1 订阅 ...

  6. Python类方法、静态方法、全局变量的使用

    一.全局变量 实现全局变量主要有两种方法:声明法和模块法 1.声明法 在文件开头声明全局变量variable,在具体函数中使用该变量时,需要事先声明 global variable,否则系统将该变量视 ...

  7. python之类的静态方法

    博主简介:原互联网大厂tencent员工,网安巨头Venustech员工,阿里云开发社区专家博主,微信公众号java基础笔记优质创作者,csdn优质创作博主,创业者,知识共享者,欢迎关注,点赞,收藏. ...

  8. python助手_python编写学习助手0

    项目原因 为了解决学习知识后不及时复习而导致遗忘的问题,准备写一个桌面助手,采用艾宾浩斯记忆法,对每次学习的内容排布复习计划. 第一步是做出最简单的文本列表,里面是待办事项,每个复习待办事项都会有符合 ...

  9. python画代码-Python教程_Python画Mandelbrot集 代码

    Python教程_Python画Mandelbrot集 代码 作者:Comet 来源: 课课家 www.kokojia.com点击数:278发布时间:2015-06-19 11:17:19 曼德勃罗集 ...

最新文章

  1. 3月Win 10美占有率超20% 免费升级是否延长受关注
  2. Java中通过命令行启动jar包时指定编码
  3. C中结构体的存储分配
  4. 扩展iQuery使其支持多种编程语言(二) – 兼编译器的语法分析简介
  5. 包打包和解析过程 unity_Unity学习—资源管理概览
  6. AJAX异步检查,检查用户名是否存在
  7. C语言一趟冒泡交换最小值,C语言单链表冒泡排序为啥以下代码实现不了?
  8. iPad mini 5外形曝光:并非全面屏设计
  9. 时间管理术第一原则总结
  10. 关于C++宏:AFX_EXT_CLASS
  11. 如果一台通用计算机的,一台计算机,之所以有相当的灵活性和通用性,能解决许多.doc...
  12. java二维码生成器
  13. 龙芯 Linux usb,使用debirf制作龙芯2F的LiveUSB
  14. 多分支表达-倍数问题
  15. ref改变样式 vue_vue.js对样式的具体操作详解
  16. “黑科技”(ipad + termius + 100 元的蓝牙键盘)
  17. 支导线手工和电脑怎么计算方位角并推算坐标
  18. 泰森多边形(Voronoi图)
  19. 机器学习中baseline,benchmark,backbone的学习记录
  20. ajax怎么解决报414,如何解决HTTP 414“请求URI太长”错误?

热门文章

  1. Java程序低手之关于泛型(Generic)
  2. Eclipse启动莫名报错处理
  3. 对报表模型项应用安全筛选器
  4. 利用XPath读取Xml文件
  5. [转载] python iter( )函数
  6. [转载] python bytearray拼接_python-4-bytes和bytearray
  7. [转载] python set 集合详解
  8. [置顶] Embedded Server:像写main函数一样写Web Server
  9. cxVerticalGrid赋值是实时更新
  10. JavaScript正则表达式补充