英文文档:

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

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

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

  2. Python内置函数之staticmethod()

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

  3. Python内置函数(60)——compile

    英文文档: compile(source, filename, mode, flags=0, dont_inherit=False, optimize=-1) Compile the source i ...

  4. Python | 内置函数(BIF)

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

  5. Python 内置函数笔记

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

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

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

  7. python内置函数详细介绍

    知识内容: 1.python内置函数简介 2.python内置函数详细介绍 一.python内置函数简介 python中有很多内置函数,实现了一些基本功能,内置函数的官方介绍文档:    https: ...

  8. lambda 表达式+python内置函数

    #函数 def f1(a,b): retrun  a+b #lambda方式,形参(a,b):返回值(a+b) f2=lambda a,b : a+b 在一些比较简单的过程计算就可以用lambda p ...

  9. Python学习:6.python内置函数

    Python内置函数 python内置函数,是随着python解释器运行而创建的函数,不需要重新定义,可以直接调用,那python的内置函数有哪些呢,接下来我们就了解一下python的内置函数,这些内 ...

随机推荐

  1. redis深入了解

    来自:https://www.cnblogs.com/lixinjie/p/a-key-point-of-redis-in-interview.html 是数据结构而非类型 很多文章都会说,redis ...

  2. Java类是如何默认继承Object的

    前言 学过Java的人都知道,Object是所有类的父类.但是你有没有这样的疑问,我并没有写extends Object,它是怎么默认继承Object的呢? 那么今天我们就来看看像Java这种依赖于虚 ...

  3. 使用CocoaPods时遇到 Permission denied 问题

    报错: Setting up CocoaPods master repo [!] /usr/bin/git clone fatal: could not create work tree dir 'm ...

  4. 【省选十连测之一】【线段树】【最小生成树之Kruskal】公路建设

    目录 题意 输入格式 输出格式 数据范围 思路 代码 题意 有n个点,m条双向道路,其中第条公路的两个端点是u[i],v[i],费用是c[i]. 现在给出q个询问,每次给定一个L和一个R,要求你只能够 ...

  5. Do Now 一个让你静心学习的APP——团队博客

    Do Now 一个让你静心学习的APP 来自油条只要半根团队的智慧凝聚的产物! 团队博客总目录: 团队作业第一周 团队作业第二周 Do Now -- 团队冲刺博客一 Do-Now-团队Scrum 冲刺 ...

  6. 创建多线程的方式:继承Thread类和实现Runnable接口

    1.通过继承Thread类的方式创建多线程(这里只是简单的代码演示创建多线程的方法) package com.baozi.exer; public class ThreadDemo { public ...

  7. xlrd模块

    import xlrdbook = xlrd.open_workbook('app_student.xls')sheet = book.sheet_by_index(0)# sheet2 = book ...

  8. esb和eai的区别

    话说SOA也推了很多年了,出现了比如ESB.SCA.jbi等各类技术和标准,乱的很.各类比较也说的云里雾里,在下理一理,按自己的观点说说. 先说说esb和eai的区别. 个人观点:esb就是eai+设 ...

  9. C++实验一

    实验结论 2-28 if...else #include <iostream> #include <stdlib.h> using namespace std; int mai ...

  10. Java 设计模式(概述)

    设计模式的三个分类                                                                                   创建型模式:对象 ...