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的内置函数,这些内 ...
随机推荐
- 更新mysql驱动5.1-47 Generated keys not requested. You need to specify Statement.RETURN_GENERATED_KEY
今天在更新mysql驱动后运行程序突然报如下错误: java.sql.SQLException: Generated keys not requested. You need to specify S ...
- java int数组任何数之间间隔不能对于指定数,内付极速排序
public static void main(String[] args) { int []arr = {300,310, 210,313,334,360,255,233,275,274,410,5 ...
- Machine Learning学习资源
引申:非原创,转载来自:https://blog.csdn.net/ptkin/article/details/50995140
- Chapter 5 : Control Structures 2 : Repetition
import java.util.*; import java.io.*; public class Loop { static Scanner console = new Scanner(Syste ...
- Linux下Shell重定向
1. 标准输入,标准输出与标准错误输出 Linux下系统打开3个文件,标准输入,标准输出,标准错误输出. 标准输入:从键盘输入数据,即从键盘读入数据. 标准输出:把数据输出到终端上. 标准错误输出:把 ...
- 百度语音合成AI
注意:不要使用Dw编辑PHP代码,会因为编码问题出错!!<?php require_once 'AipSpeech.php'; // 你的 APPID AK SK const APP_ID = ...
- go、java or c艹 引用的本质
在底层,引用变量由指针按照指针常量的方式实现 即一个指针常量,和一些解引用等的封装: 合到一起实现了指针这么一种形式. 用指针和引用编译到了汇编层面应该是一样的.
- Prometheus — Process-exporter进程监控
由于我们常用的node_exporter并不能覆盖所有监控项,这里我们使用Process-exporter 对进程进行监控. 安装process-exporter wget https://githu ...
- Java for Andriod 第二周学习总结
第四章 学习时遇到的问题或新知识点: 1. 构造方法.每个类至少有一个构造方法,且构造方法必须的名称必须与类名相同. 2. Varargs.允许方法拥有一个可变长度的参数列表. 3. 对象的内存分配. ...
- Java GC相关知识
Java堆的分类 分为两类:YoungGen和OldGen.其中,YoungGen分为三部分:eden,from survivor和to survivor,比例默认是:8:1:1 PermGen不属于 ...