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

作者:lincappu 时间:2022-11-07 07:25:11 

英文文档:

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
你好!

来源:https://www.cnblogs.com/lincappu/p/8145385.html

标签:Python,staticmethod,装饰器
0
投稿

猜你喜欢

  • Python中使用Boolean操作符做真值测试实例

    2021-05-31 01:18:19
  • Python实现Opencv cv2.Canny()边缘检测

    2022-12-01 13:26:37
  • 自学python求已知DNA模板的互补DNA序列

    2022-07-05 13:24:56
  • 用Python Turtle画棵樱花树送给自己

    2022-06-30 10:16:47
  • Linux中安装Python的交互式解释器IPython的教程

    2023-09-14 05:50:06
  • Mobile Web下的编码设计

    2010-01-28 10:42:00
  • SQL Server 2016 CTP2.3 的关键特性总结

    2024-01-19 12:30:33
  • 2022最新版MySQL 8.0.30 安装及配置教程(小白入门)

    2024-01-28 16:49:49
  • 一文弄懂Pytorch的DataLoader, DataSet, Sampler之间的关系

    2022-06-27 14:21:53
  • 深入理解python try异常处理机制

    2023-01-06 00:51:35
  • 如何优化SQL语句(全)

    2024-01-27 06:12:59
  • Oracle与MySQL删除字段时对索引和约束的处理

    2008-12-26 16:41:00
  • Python报错:ModuleNotFoundError的解决办法

    2023-02-19 10:53:09
  • CSS3中的box-sizing属性

    2010-04-05 21:52:00
  • 解决缩小图标变样问题

    2007-10-08 19:13:00
  • 工作需要写的一个js拖拽组件

    2024-04-08 10:53:20
  • 使用Python实现从各个子文件夹中复制指定文件的方法

    2023-11-09 12:04:05
  • SQLServer2008提示评估期已过解决方案

    2024-01-22 02:01:28
  • Oracle 11g安装错误提示未找到wfmlrsvcapp.ear的解决方法

    2023-07-14 14:31:52
  • 浅谈ASP自动采集程序及入库

    2007-08-17 11:25:00
  • asp之家 网络编程 m.aspxhome.com