Python基于staticmethod装饰器标示静态方法


Posted in Python onOctober 17, 2020

英文文档:

staticmethod(function)

Return a static method for function.

A static method does not receive an implicit first argument.

The @staticmethod form is a function decorator ? see the description of function definitions in Function definitions for details.

It can be called either on the class (such as C.f()) or on an instance (such as C().f()). The instance is ignored except for its class.

标示方法为静态方法的装饰器

说明:

1. 类中普通的方法,实际上既可以被类直接调用也可以被类的实例对象调用,但是被实例对象调用的时候,要求方法至少有一个参数,而且调用时会将实例对象本身传给第一个参数

>>> class Student(object):
  def __init__(self,name):
    self.name = name
  def sayHello(lang):
    print(lang)
    if lang == 'en':
      print('Welcome!')
    else:
      print('你好!')
 
  
>>> Student.sayHello
<function Student.sayHello at 0x02AC7810>
>>> a = Student('Bob')
>>> a.sayHello
<bound method Student.sayHello of <__main__.Student object at 0x02AD03F0>>
>>> Student.sayHello('en') # 类调用的时候,将'en'传给了lang参数
en
Welcome!

>>> a.sayHello() # 类实例对象调用的时候,将对象本身自动传给了lang参数,不能再接收参数
<__main__.Student object at 0x02AD03F0>
你好!
  >>> a.sayHello('en')  Traceback (most recent call last):  File "<pyshell#7>", line 1, in <module>  a.sayHello('en')  TypeError: sayHello() takes 1 positional argument but 2 were given

2. staticmethod函数功能就是将一个方法定义成类的静态方法,正确的方法是使用 @staticmethod装饰器,这样在实例对象调用的时候,不会把实例对象本身传入静态方法的第一个参数了。

# 使用装饰器定义静态方法
>>> class Student(object):
  def __init__(self,name):
    self.name = name
  @staticmethod
  def sayHello(lang):
    print(lang)
    if lang == 'en':
      print('Welcome!')
    else:
      print('你好!')

      
>>> Student.sayHello('en') #类调用,'en'传给了lang参数
en
Welcome!

>>> b = Student('Kim') #类实例对象调用,不再将类实例对象传入静态方法
>>> b.sayHello()
Traceback (most recent call last):
 File "<pyshell#71>", line 1, in <module>
  b.sayHello()
TypeError: sayHello() missing 1 required positional argument: 'lang'

>>> b.sayHello('zh') #类实例对象调用,'zh'传给了lang参数
zh
你好!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python二分法搜索算法实例分析
May 11 Python
Python中方法链的使用方法
Feb 23 Python
Python基础篇之初识Python必看攻略
Jun 23 Python
python快速建立超简单的web服务器的实现方法
Feb 17 Python
python 获取毫秒数,计算调用时长的方法
Feb 20 Python
关于Python作用域自学总结
Jun 10 Python
详解Python 多线程 Timer定时器/延迟执行、Event事件
Jun 27 Python
python采集百度搜索结果带有特定URL的链接代码实例
Aug 30 Python
Python关于__name__属性的含义和作用详解
Feb 19 Python
python向xls写入数据(包括合并,边框,对齐,列宽)
Feb 02 Python
Python代码,能玩30多款童年游戏!这些有几个是你玩过的
Apr 27 Python
Python编写冷笑话生成器
Apr 20 Python
详解python算法常用技巧与内置库
Oct 17 #Python
Python 操作SQLite数据库的示例
Oct 16 #Python
python Selenium 库的使用技巧
Oct 16 #Python
用Python进行websocket接口测试
Oct 16 #Python
python如何控制进程或者线程的个数
Oct 16 #Python
python利用 keyboard 库记录键盘事件
Oct 16 #Python
python实现快速文件格式批量转换的方法
Oct 16 #Python
You might like
php二维数组排序与默认自然排序的方法介绍
2013/04/27 PHP
调整PHP的性能
2013/10/30 PHP
PHP_NETWORK_GETADDRESSES: GETADDRINFO FAILED问题解决办法
2014/05/04 PHP
PHP检测字符串是否为UTF8编码的常用方法
2014/11/21 PHP
Symfony模板的快捷变量用法实例
2016/03/17 PHP
Laravel日志用法详解
2016/10/09 PHP
PHP的imageTtfText()函数深入详解
2021/03/03 PHP
JQuery 网站换肤功能实现代码
2009/11/02 Javascript
JavaScript自动设置IFrame高度的小例子
2013/06/08 Javascript
JavaScript中return false的用法
2015/03/12 Javascript
探讨跨域请求资源的几种方式(总结)
2016/12/02 Javascript
基于pako.js实现gzip的压缩和解压功能示例
2017/06/13 Javascript
js 获取json数组里面数组的长度实例
2017/10/31 Javascript
移动端(微信等使用vConsole调试console的方法
2019/03/05 Javascript
vuex管理状态仓库使用详解
2020/07/29 Javascript
[02:41]DOTA2亚洲邀请赛小组赛第三日 赛事回顾
2015/02/01 DOTA
[01:12]DOTA2 2015年秋季互动指南
2015/11/10 DOTA
[03:36]2015国际邀请赛第二日现场精彩集锦
2015/08/06 DOTA
[51:53]完美世界DOTA2联赛决赛日 Inki vs LBZS 第二场 11.08
2020/11/10 DOTA
Python学习小技巧之列表项的排序
2017/05/20 Python
Python基于回溯法子集树模板解决马踏棋盘问题示例
2017/09/11 Python
Python中的pack和unpack的使用
2018/03/12 Python
python中计算一个列表中连续相同的元素个数方法
2018/06/29 Python
Django如何实现网站注册用户邮箱验证功能
2019/08/14 Python
15行Python代码实现免费发送手机短信推送消息功能
2020/02/27 Python
利用python制作拼图小游戏的全过程
2020/12/04 Python
HTML5 本地存储之如果没有数据库究竟会怎样
2013/04/25 HTML / CSS
丝芙兰巴西官方商城:SEPHORA巴西
2016/10/31 全球购物
Trunki英国官网:儿童坐骑式行李箱
2017/05/30 全球购物
Harman Audio官方商店:购买JBL、Harman Kardon、Infinity和AKG
2019/12/05 全球购物
文秘专业毕业生就业推荐信
2013/11/08 职场文书
大学四年学习的自我评价分享
2013/12/09 职场文书
给学校的建议书
2014/03/12 职场文书
销售员自我评价
2015/03/11 职场文书
业务员岗位职责范本
2015/04/03 职场文书
优秀共产党员主要事迹材料
2015/11/05 职场文书