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 相关文章推荐
py2exe 编译ico图标的代码
Mar 08 Python
Python语法快速入门指南
Oct 12 Python
Python的几个高级语法概念浅析(lambda表达式闭包装饰器)
May 28 Python
Python 常用的安装Module方式汇总
May 06 Python
django 2.0更新的10条注意事项总结
Jan 05 Python
Python爬虫框架Scrapy实例代码
Mar 04 Python
python高级特性和高阶函数及使用详解
Oct 17 Python
matplotlib实现热成像图colorbar和极坐标图的方法
Dec 13 Python
详解pandas DataFrame的查询方法(loc,iloc,at,iat,ix的用法和区别)
Aug 02 Python
python 非线性规划方式(scipy.optimize.minimize)
Feb 11 Python
OpenCV+python实现膨胀和腐蚀的示例
Dec 21 Python
浅谈Python类的单继承相关知识
May 12 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的相似度计算函数:levenshtein的使用介绍
2013/04/15 PHP
实测在class的function中include的文件中非php的global全局环境
2013/07/15 PHP
php实现curl模拟ftp上传的方法
2015/07/29 PHP
关于JavaScript的gzip静态压缩方法
2007/01/05 Javascript
php对mongodb的扩展(小试牛刀)
2012/11/11 Javascript
Jquery跳到页面指定位置的方法
2014/05/12 Javascript
javascript实现状态栏文字首尾相接循环滚动的方法
2015/07/22 Javascript
跟我学习javascript的var预解析与函数声明提升
2015/11/16 Javascript
Node.js返回JSONP详解
2016/05/18 Javascript
js实现文字向上轮播功能
2017/01/13 Javascript
Angular2 PrimeNG分页模块学习
2017/01/14 Javascript
mint-ui的search组件在键盘显示搜索按钮的实现方法
2017/10/27 Javascript
JavaScript实现重力下落与弹性效果的方法分析
2017/12/20 Javascript
js/jQuery实现全选效果
2019/06/17 jQuery
vue 导航锚点_点击平滑滚动,导航栏对应变化详解
2020/08/10 Javascript
vue 将多个过滤器封装到一个文件中的代码详解
2020/09/05 Javascript
[57:24]LGD vs VGJ.T 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
Python入门篇之对象类型
2014/10/17 Python
浅谈Python类的__getitem__和__setitem__特殊方法
2016/12/25 Python
python计算两个数的百分比方法
2018/06/29 Python
对python中数组的del,remove,pop区别详解
2018/11/07 Python
Python使用random.shuffle()打乱列表顺序的方法
2018/11/08 Python
Python图像处理之图片文字识别功能(OCR)
2019/07/30 Python
mac 上配置Pycharm连接远程服务器并实现使用远程服务器Python解释器的方法
2020/03/19 Python
python用opencv完成图像分割并进行目标物的提取
2020/05/25 Python
Python通过队列来实现进程间通信的示例
2020/10/14 Python
python 制作网站筛选工具(附源码)
2021/01/21 Python
澳大利亚UGG工厂直销:Australian Ugg Boots
2017/10/14 全球购物
大学生学习自我评价
2014/01/13 职场文书
学校运动会霸气口号
2014/06/07 职场文书
2014年教师工作总结
2014/11/10 职场文书
敲诈同学钱财检讨书范文
2014/11/18 职场文书
捐款通知怎么写
2015/04/24 职场文书
2015年校务公开工作总结
2015/05/26 职场文书
小学新课改心得体会
2016/01/22 职场文书
详解python网络进程
2021/06/15 Python