Python内置函数(60)——staticmethod
英文文档:
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内置函数(60)——staticmethod的更多相关文章
- Python内置函数(65)——staticmethod
英文文档: staticmethod(function) Return a static method for function. A static method does not receive a ...
- Python内置函数之staticmethod()
staticmethod(function)返回函数的静态方法.一般来说,实例对象调用类方法不用传入参数,因为实例对象本身隐式的作为第一个参数传入了.而采用静态方法之后,实例对象在调用类方法时必须传入 ...
- Python内置函数(60)——compile
英文文档: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) Compile the source i ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- python内置函数大全(分类)
python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...
- python内置函数详细介绍
知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档: https: ...
- lambda 表达式+python内置函数
#函数 def f1(a,b): retrun a+b #lambda方式,形参(a,b):返回值(a+b) f2=lambda a,b : a+b 在一些比较简单的过程计算就可以用lambda p ...
- Python学习:6.python内置函数
Python内置函数 python内置函数,是随着python解释器运行而创建的函数,不需要重新定义,可以直接调用,那python的内置函数有哪些呢,接下来我们就了解一下python的内置函数,这些内 ...
随机推荐
- Web系统中Mic设备的应用实例
>>>>>>>>>>>>>>>>>>>>>>>>> ...
- Cookies的使用之购物车的实现
Cookies的使用之购物车实现 最近学习了JSON对象之后,发现Cookies的使用更加的灵活方便了.ps:JSON不是JS.可以这么理解,JSON 是 JS 对象的字符串表示法,它使用文本表示一个 ...
- 使用 Fetch
原文链接:https://css-tricks.com/using-fetch/. 本文介绍了Fetch基本使用方法及zlFetch库的使用 无论用JavaScript发送或获取信息,我们都会用到Aj ...
- Java - Multithreading zz
Java is a multi-threaded programming language which means we can develop multi-threaded program usin ...
- Python 中关于Random的使用方法
Random 在 Python 中的使用方法: 1.random.random(): 会随机生成0-1之间的小数 例如: 2.random.uniform(min,max): 会随机生成 min - ...
- BZOJ.5467.[PKUWC2018]Slay the Spire(DP)
LOJ BZOJ 洛谷 哪张能力牌能乘攻击啊,太nb了叭 显然如果有能力牌,那么应该选最大的尽可能的打出\(k-1\)张. 然后下面说的期望都是乘总方案数后的,即所有情况的和.然后\(w_i\)统一用 ...
- Spark集群部署(standLone)模式
安装部署: 1. 配置spark为1个master,2个slave的独立集群(Standlone)模式, 可以在VMWare中构建3台运行Ubuntu的机器作为服务器: master主机配置如下: ...
- ubuntu 14.04 rabbitmq集群部署
1.准备机器,我这边准备的是三台ubuntu14.04 机器主机名不能相同,不然节点冲突 2.安装rabbitmq 3.修改hosts文件 root@abc-web-04:~# vim /etc/ho ...
- Syntax error, insert "}" to complete ClassBody错误解决
Syntax error, insert "}" to complete ClassBody 报该错误是因为我从网页上粘贴了别人的代码,并没有发现什么异常但还是编译器报红叉. 解决 ...
- STS(Spring Tool Suite)下SSM(Spring+SpringMVC+Mybatis)框架搭建(二)
继完成controller配置并使用controller实现页面跳转,现连接数据库进行登录. 在SSM框架中,使用Mybatis与数据库连接,因此需要配置关于mybatis的配置. 废话少说直接开始: ...