英文文档:

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的更多相关文章

  1. Python内置函数(60)——staticmethod

    英文文档: staticmethod(function) Return a static method for function. A static method does not receive a ...

  2. Python内置函数之staticmethod()

    staticmethod(function)返回函数的静态方法.一般来说,实例对象调用类方法不用传入参数,因为实例对象本身隐式的作为第一个参数传入了.而采用静态方法之后,实例对象在调用类方法时必须传入 ...

  3. Python内置函数(65)——type

    英文文档: class type(object) class type(name, bases, dict) With one argument, return the type of an obje ...

  4. Python补充--Python内置函数清单

    Python内置函数 Python内置(built-in)函数随着python解释器的运行而创建.在Python的程序中,你可以随时调用这些函数,不需要定义.最常见的内置函数是: print(&quo ...

  5. Python | 内置函数(BIF)

    Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...

  6. python 内置函数和函数装饰器

    python内置函数 1.数学相关 abs(x) 取x绝对值 divmode(x,y) 取x除以y的商和余数,常用做分页,返回商和余数组成一个元组 pow(x,y[,z]) 取x的y次方 ,等同于x ...

  7. Python 内置函数笔记

    其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...

  8. python内置函数,匿名函数

    一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...

  9. python内置函数大全(分类)

    python内置函数大全 python内建函数 最近一直在看python的document,打算在基础方面重点看一下python的keyword.Build-in Function.Build-in ...

随机推荐

  1. react-todoMVC脚手架

    webpack.config.js var path = require('path'); // node中的 路径解析的模块 const HtmlWebpackPlugin =require('ht ...

  2. IDEA 使用tomcat7-maven-plugin

    使用了这个插件就不需要配置tomcat了,直接用maven去run就行 配置方法:pom里添加:(之所以用tomcat7是因为如果直接用依赖下载很难下载到tomcat8-maven-plugin,详情 ...

  3. .net core2.0半年的使用经验之前言

    目录 前言 使用dapper做数据层调用 使用T4模板生成 使用缓存 使用swagger做接口文档(非restful) 使用identity做身份认证 使用jwt做身份认证 使用CORS跨域 调用we ...

  4. firemonkey EDit 改变颜色

    PS:本来不应该有多难,结果折腾了半天, firemonkey EDit  Canvas 按需绘颜色 procedure TForm.EditPaint(Sender: TObject; Canvas ...

  5. USB协议基础知识笔记

    usb协议 功能层: 控制传输.中断传输.块传输(批量传输).同步传输 设备层:管理USB设备.分配地址.读取设备描述符 总线接口层:采用NRZI编码-反向非归零编码(0将前一个电平翻转,1不翻转) ...

  6. 方法的重写与重载的区别(Override与Overload)。重载的方法是否可以改变返回值的类型

    方法的重写(Override)与重载(Overload)的区别.重载的方法是否可以改变返回值的类型?[基础] 解释: 方法的重写overriding和重载Overloading是Java多态性的不同表 ...

  7. Asp.Net Core 2.0 项目实战(11) 基于OnActionExecuting全局过滤器,页面操作权限过滤控制到按钮级

    1.权限管理 权限管理的基本定义:百度百科. 基于<Asp.Net Core 2.0 项目实战(10) 基于cookie登录授权认证并实现前台会员.后台管理员同时登录>我们做过了登录认证, ...

  8. from提交表单后 数据提交到后台 但不跳转页面 可用iframe

    可以页面事先加载被隐藏的iframe标签,或者等到需要的时候通过js生成,再提交,提交之前,form的target指向iframe(我是要实现新页面生成的时候程半透明状态,所以用了后者的方法) 代码如 ...

  9. 【Zabbix】 Zabbix表结构说明【转载】

    本文转自[https://www.cnblogs.com/shhnwangjian/p/5484352.html] 参考文[https://www.cnblogs.com/learningJAVA/p ...

  10. 【Python】 更多数据类型collections&简易数据文件shelve

    ■collections collections在python内建的数据类型基础上新增一些实用的数据类型,其目的在于增加代码的可读性?(虽然我自己没怎么用过..) ① deque 双端队列 q = d ...