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装饰器使用示例及实际应用例子
Mar 06 Python
python编程开发之日期操作实例分析
Nov 13 Python
浅谈Python使用Bottle来提供一个简单的web服务
Dec 27 Python
Python闭包之返回函数的函数用法示例
Jan 27 Python
pandas.dataframe中根据条件获取元素所在的位置方法(索引)
Jun 07 Python
python使用循环打印所有三位数水仙花数的实例
Nov 13 Python
Python3 itchat实现微信定时发送群消息的实例代码
Jul 12 Python
使用Fabric自动化部署Django项目的实现
Sep 27 Python
Keras load_model 导入错误的解决方式
Jun 09 Python
python一些性能分析的技巧
Aug 30 Python
python如何提升爬虫效率
Sep 27 Python
Python 制作查询商品历史价格的小工具
Oct 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
最省空间的计数器
2006/10/09 PHP
CI框架中集成CKEditor编辑器的教程
2014/06/09 PHP
php利用cookies实现购物车的方法
2014/12/10 PHP
Yii2 中实现单点登录的方法
2018/03/09 PHP
THINKPHP-Apache服务器中使用Alias虚拟目录URL重写 隐藏index.php
2021/03/09 PHP
JS 文件本身编码转换 图文教程
2009/10/12 Javascript
JavaScript 学习笔记(五)
2009/12/31 Javascript
javascript向后台传送相同属性的参数即数组参数
2014/02/17 Javascript
使用正则表达式的格式化与高亮显示json字符串
2014/12/03 Javascript
JavaScript学习笔记之ES6数组方法
2016/03/25 Javascript
JS实现消息来时让网页标题闪动效果的方法
2016/04/20 Javascript
使用原生js写ajax实例(推荐)
2017/05/31 Javascript
Easyui Datagrid自定义按钮列(最后面的操作列)
2017/07/13 Javascript
Angular弹出模态框的两种方式
2017/10/19 Javascript
nodejs实现大文件(在线视频)的读取
2020/10/16 NodeJs
使用vue-cli3 创建vue项目并配置VS Code 自动代码格式化 vue语法高亮问题
2019/05/14 Javascript
Bootstrap table 服务器端分页功能实现方法示例
2020/06/01 Javascript
如何实现echarts markline标签名显示自己想要的
2020/07/20 Javascript
python利用elaphe制作二维条形码实现代码
2012/05/25 Python
python基础教程之字典操作详解
2014/03/25 Python
浅谈python中的面向对象和类的基本语法
2016/06/13 Python
如何运行带参数的python脚本
2019/11/15 Python
利用Python的sympy包求解一元三次方程示例
2019/11/22 Python
Python反爬虫伪装浏览器进行爬虫
2020/02/28 Python
Python reversed函数及使用方法解析
2020/03/17 Python
Sephora丝芙兰印尼官方网站:购买化妆品和护肤品
2018/07/02 全球购物
复古服装:RetroStage
2019/05/10 全球购物
记账会计岗位职责
2014/06/16 职场文书
关于教师节的演讲稿
2014/09/04 职场文书
委托书的写法
2014/09/16 职场文书
2014年民政局关于保密工作整改措施
2014/09/19 职场文书
出差报告范文
2014/11/06 职场文书
考研经验交流会策划书
2015/11/02 职场文书
初中班长竞选稿
2015/11/20 职场文书
Matlab求解数组中的最大值及它所在的具体位置
2021/04/16 Python
sql注入报错之注入原理实例解析
2022/06/10 MySQL