@classmethod装饰器 (The @classmethod Decorator)

The @classmethod decorator is an inbuilt function decorator that gets evaluated after the function is defined. The result of the evaluation shadows the function definition. The @classmethod's first argument is always a class cls, similar to an instance method receiving self as its first argument.

@classmethod装饰器是一个内置的函数装饰器,在定义函数后会对其进行评估。 评估结果遮盖了功能定义。 @classmethod的第一个参数始终是cls类,类似于将self作为其第一个参数的实例方法。

Syntax:

句法:

    Class ABC(object):
@classmethod
def function(cls, arg1, ...):
...

  • Exists to create class methods that are passed with the actual class object within the function call.

    存在以创建在函数调用中与实际类对象一起传递的类方法。

  • Bound to the class and not to an instance.

    绑定到类而不是实例。

  • Can modify the class state and that would be applied across all the instances.

    可以修改类状态,并将其应用于所有实例。

@staticmethod装饰器 (The @staticmethod Decorator)

@staticmethods, similar to class methods, are methods that are bound to class rather than its object. However, they do not require a class instance creation. So, are not dependent on the state of the object.

与类方法类似, @staticmethods是绑定到类而不是对象的方法。 但是,它们不需要创建类实例。 因此,不依赖于对象的状态。

Syntax:

句法:

    Class ABC(object):
@staticmethod
def function(arg1, arg2, ...):
...

  • Bound to the class and not to an instance

    绑定到类而不是实例

  • Cannot modify the class state

    无法修改类状态

@classmethod和@staticmethod之间的比较 (Comparison between @classmethod and @staticmethod)

Class method Static method
Takes cls as first parameter Needs no specific parameters
Can access or modify the class state Cannot access the class state
They must have parameters Knows nothing about the class state. Are similar to utility methods.
类方法 静态方法
以cls作为第一个参数 不需要特定参数
可以访问或修改类状态 无法访问类状态
他们必须有参数 对类状态一无所知。 与实用程序方法相似。

@classmethod和@staticmethod的示例实现 (Example implementation of @classmethod and @staticmethod)

class City:
def __init__(self, zip_code, name):
self.zip_code = name
self.name = name
# a class method to create a city object.
@classmethod
def city_name(cls, zip_code, name):
return cls(zip_code, name)
# a static method to check if a city is capital or not
@staticmethod
def isCapital(city_name):
if city_name == 'bengaluru':
return True
if __name__ == '__main__':
bengaluru = City(560086, 'bengaluru')
mysuru = City.city_name(560111, 'mysuru')
print("city is {}".format(bengaluru.name))
print("city is {}".format(mysuru.name))
print("Bengaluru is capital :{}".format(City.isCapital('bengaluru')))

Output

输出量

city is bengaluru
city is mysuru
Bengaluru is capital : True

翻译自: https://www.includehelp.com/python/staticmethod-vs-classmethod.aspx

Python中@staticmethod和@classmethod之间的区别相关推荐

  1. python中的classmethod_面试题:python 中 staticmethod 和 classmethod有什么区别

    面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...

  2. python中@staticmethod、@classmethod和实例方法

    1.形式上的异同点: 在形式上,Python中:实例方法必须有self,类方法用@classmethod装饰必须有cls,静态方法用@staticmethod装饰不必加cls或self,如下代码所示: ...

  3. python中列表字典元组之间的区别_python之字典、元组、列表的区别

    1.列表:专门用于存储一串信息,列表用中括号[]定义,数据之间使用逗号,分隔 list_0=['a','b','c'] 列表的特点: 1)可修改.可增加.可删除 2)列表是有序的,可以遍历 3)可以将 ...

  4. Python中@staticmethod和@classmethod的作用和区别

    简单介绍一下两者的区别: 对于一般的函数test(x),它跟类和类的实例没有任何关系,直接调用test(x)即可 #!/usr/bin/python # -*- coding:utf-8 -*-def ...

  5. python __import__和import区别_Python中import 与__import__() 之间的区别比较

    本篇文章给大家带来的内容是关于Python中import 与__import__() 之间的区别比较,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. 首先来说一下两者的区别: impo ...

  6. python中的return和print的区别_python中return和print的区别(详细)

    Huskiesir python最近正在研究.今天,我面临一个问题,那就是,返回和印刷的区别.双方都能输出结果.的区别是什么?闲话少说,看下面的例子.# Code1: def break_words( ...

  7. python观察日志(part20)--列表中加号,extend,append之间的区别

    学习笔记,仅供参考,有错必纠 列表中"+"加号,extend,append之间的区别 extend extend函数用于在列表末尾一次性追加另一个序列中的多个值. append a ...

  8. Python中str()与repr()函数的区别——repr() 的输出追求明确性,除了对象内容,还需要展示出对象的数据类型信息,适合开发和调试阶段使用...

    Python中str()与repr()函数的区别 from:https://www.jianshu.com/p/2a41315ca47e 在 Python 中要将某一类型的变量或者常量转换为字符串对象 ...

  9. Python中怎样改变集合之间的关系?

    Python中怎样改变集合之间的关系?数学中,两个集合关系的常见操作包括:交集.并集.差集.补集.设A,B是两个集合,集合关系的操作介绍如下: 交集是指属于集合A且属于集合B的元素所组成的集合, 并集 ...

最新文章

  1. 10 个案例分享几个 Python 可视化小技巧,助你绘制高质量图表
  2. 我的数字万用表怎么了?
  3. P3193-[HNOI2008]GT考试【KMP,dp,矩阵乘法】
  4. 真!长!啊!中国校名最长的学校竟然有55个字!
  5. 深入浅出JS:Two
  6. thinkphp [数据分页]
  7. 用adb pull命令从android系统中读取文件失败的原因及解决办法
  8. 【AI志愿超强攻略】中国高校人工智能专业最全院校排名课程对比
  9. 活力四射MSNMessenger(转)
  10. C语言入门题库——求数列2/1+3/2+5/3......的和
  11. 了解前沿信息科技 做好学习就业规划
  12. msxml3.dll 错误 '800c0008'
  13. 证书有效性验证、根证书
  14. 计算机编程年纪大了会被淘汰吗,这些“吃青春饭”的专业,年纪大了容易被淘汰,应届生更受欢迎...
  15. 读书笔记: Cartesian Impedance Control of Redundant and Flexible-Joint Robots, Section 1
  16. iphone屏幕尺寸总结
  17. 差分隐私(Differential Privacy)定义及其理解
  18. 基于Springboot+vue的服装销售购物商城网站 毕业设计源码
  19. 微信小程序-实现音乐播放页(flex)
  20. 《惢客创业日记》2020.01.14(周二)从新学习《长征》

热门文章

  1. ubuntu vim php配置文件在哪,ubuntu vim的配置文件在哪
  2. linux没有root密码xshell,LINUX终端免密登陆(以Xshell为例)
  3. zTree 优秀的jquery树插件
  4. 4. HTML表单标签
  5. 替换富文本里的px为rem
  6. OP AMP - 反馈理论在运放中的应用
  7. springMVC开启声明式事务实现操作日志记录
  8. mysql复制模式第二部分-----双主模式
  9. NOIP 2012 Day2
  10. Spring-framework应用程序启动loadtime源码分析笔记(二)——@Transactional