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提取页面内url列表的方法
May 25 Python
Python中类型关系和继承关系实例详解
May 25 Python
Python内置模块logging用法实例分析
Feb 12 Python
小白入门篇使用Python搭建点击率预估模型
Oct 12 Python
Python中出现IndentationError:unindent does not match any outer indentation level错误的解决方法
Apr 18 Python
Flask框架学习笔记之消息提示与异常处理操作详解
Aug 15 Python
python爬虫 urllib模块反爬虫机制UA详解
Aug 20 Python
python conda操作方法
Sep 11 Python
Python基本语法之运算符功能与用法详解
Oct 22 Python
Python如何使用PIL Image制作GIF图片
May 16 Python
Python持续监听文件变化代码实例
Jul 22 Python
如何用Python进行时间序列分解和预测
Mar 01 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控制用户的浏览器--ob*函数的使用说明
2007/03/16 PHP
php调用dll的实例操作动画与代码分享
2012/08/14 PHP
php清空(删除)指定目录下的文件,不删除目录文件夹的实现代码
2014/09/04 PHP
php生成圆角图片的方法
2015/04/07 PHP
超级强大的表单验证
2006/06/26 Javascript
Ajax::prototype 源码解读
2007/01/22 Javascript
JavaScript 原型与继承说明
2010/06/09 Javascript
Ext对基本类型的扩展 ext,extjs,format
2010/12/25 Javascript
IE图片缓存document.execCommand(&quot;BackgroundImageCache&quot;,false,true)
2011/03/01 Javascript
Moment.js 不容错过的超棒Javascript日期处理类库
2012/04/15 Javascript
js判断屏幕分辨率的代码
2013/07/16 Javascript
jQuery手机拨号界面特效代码分享
2015/08/27 Javascript
nodejs修复ipa处理过的png图片
2016/02/17 NodeJs
js仿支付宝填写支付密码效果实现多方框输入密码
2016/03/09 Javascript
vue-cli整合vuex的时候,修改actions和mutations,实现热部署的方法
2018/09/19 Javascript
详解webpack引入第三方库的方式以及注意事项
2019/01/15 Javascript
微信小程序传值以及获取值方法的详解
2019/04/29 Javascript
vue项目添加多页面配置的步骤详解
2019/05/22 Javascript
node读写Excel操作实例分析
2019/11/06 Javascript
js在HTML的三种引用方式详解
2020/08/29 Javascript
轻松理解Python 中的 descriptor
2017/09/15 Python
python爬虫爬取淘宝商品信息
2018/02/23 Python
python实现基于信息增益的决策树归纳
2018/12/18 Python
Python使用Shelve保存对象方法总结
2019/01/28 Python
Python批量生成特定尺寸图片及图画任意文字的实例
2019/01/30 Python
Python3模拟登录操作实例分析
2019/03/12 Python
连接pandas以及数组转pandas的方法
2019/06/28 Python
python ctypes库2_指定参数类型和返回类型详解
2019/11/19 Python
用python给csv里的数据排序的具体代码
2020/07/17 Python
Oroton中国官网:澳洲知名奢侈配饰品牌
2017/03/26 全球购物
青春演讲稿范文
2014/05/08 职场文书
物理系毕业生自荐书
2014/06/13 职场文书
竞选学委演讲稿
2014/09/13 职场文书
放射科岗位职责
2015/02/14 职场文书
幼儿园教师心得体会范文
2016/01/21 职场文书
Nginx配置文件详解以及优化建议指南
2021/09/15 Servers