Python内置函数(65)——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内置函数(65)——staticmethod的更多相关文章
- Python内置函数(60)——staticmethod
英文文档: staticmethod(function) Return a static method for function. A static method does not receive a ...
- Python内置函数之staticmethod()
staticmethod(function)返回函数的静态方法.一般来说,实例对象调用类方法不用传入参数,因为实例对象本身隐式的作为第一个参数传入了.而采用静态方法之后,实例对象在调用类方法时必须传入 ...
- Python内置函数(65)——type
英文文档: class type(object) class type(name, bases, dict) With one argument, return the type of an obje ...
- Python补充--Python内置函数清单
Python内置函数 Python内置(built-in)函数随着python解释器的运行而创建.在Python的程序中,你可以随时调用这些函数,不需要定义.最常见的内置函数是: print(&quo ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- python 内置函数和函数装饰器
python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
- python内置函数大全(分类)
python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...
随机推荐
- c++中回调函数和函数指针的使用
#include "stdafx.h" #include <iostream> //#include <string> using namespace st ...
- 云计算之路-阿里云上:针对 docker swarm 故障的部署调整以及应急措施
针对这周 docker swarm 集群的频繁故障(详见故障一 .故障二.故障三),我们今天对 docker swarm 集群的部署进行了如下调整. 将 docker engine 由 “17.12 ...
- 使用text-overflow: ellipsis 处理文本溢的一个小问题
今天在做一个自定义 select多选 搜索的时候,有这样子的一个需求: 1.点击自定义的一个 选项内容框,下拉可多选项 2.多选项不允许换行,且父溢出拆剪,(单行)溢出部分使用 "...&q ...
- 纯css3打造瀑布流布局
纯css3打造瀑布流布局 原理: 1.column-count 把div中的文本分为多少列 2.column-width 规定列宽 3.column-gap 规定列间隙 4.break-inside: ...
- delete操作字符串会报错吗?
其实是不会的."delete只能对对象属性进行操作"的说法是错误的,严谨来说,delete可以对任意变量类型进行操作,只不过只有对对象的属性才能操作成功,对其他变量类型操作失败罢了 ...
- Python基础-week01
本节内容摘要:http://www.cnblogs.com/Jame-mei Python介绍 Python是怎么样的语言? Python 2 or 3? 安装 Hello World程序 变量 用户 ...
- 简单docker镜像修改方式
• 创建Dockerfile,文件内容如下: FROM nps:v1.0.1 ENTRYPOINT ["/usr/bin/init.sh"] • 启动基础镜像:docker run ...
- 关于IM的一些思考与实践
上一篇简单的实现了一个聊天网页,但这个太简单,消息全广播,没有用户认证和已读未读处理,主要的意义是走通了websocket-sharp做服务端的可能性.那么一个完整的IM还需要实现哪些部分? 一.发消 ...
- java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListene解决办法
照着以前写的项目搭一个框架,项目用到的是ssm,spring+spring mvc+mybatis,由Eclipse转战IDEA上,项目的文件夹有一些改变,在之前的小项目中喜欢把lib文件夹放在项目根 ...
- react开发
webpack.config.js var webpack=require("webpack"); var htmlWebpackPlugin=require('html-webp ...