python 中 staticmethod 和 classmethod有什么区别
面试中经常会问到staticmethod 和 classmethod有什么区别?
首先看下官方的解释:
staticmethod:
class staticmethod
staticmethod(function) -> method
Convert a function to be a static method.
A static method does not receive an implicit first argument. To declare a static method, use this idiom:
class C:
def f(arg1, arg2, ...): ...
f = staticmethod(f)It can be called either on the class (e.g. C.f()) or on an instance (e.g. C().f()). The instance is ignored except for its class.
Static methods in Python are similar to those found in Java or C++. For a more advanced concept,
它的一个作用就是将一个一个类的函数转为一个静态函数。静态函数的作用和java,c++的静态函数类似,作用一些全局变量等。
classmethod:
class classmethod
classmethod(function) -> method
Convert a function to be a class method.
A class method receives the class as implicit first argument, just like an instance method receives the instance. To declare a class method, use this idiom:
class C: def f(cls, arg1, arg2, ...): ... f = classmethod(f)
It can be called either on the class (e.g. C.f()) or on an instance (e.g. C().f()). The instance is ignored except for its class. If a class method is called for a derived class, the derived class object is passed as the implied first argument.
Class methods are different than C++ or Java static methods. If you want those, see the staticmethod builtin.
classmethod 和 c++,java的类方法不同。类方法的参数是一个类,实例方法的参数是一个实例。
例如下面的程序:
class A:
@staticmethod
def a():
print "this is a" @classmethod
def b(rty):
print(rty)
print "this is b" testa = A()
testa.a()
testa.b() # 输出为:
# this is a
# __main__.A
# this is b
其中的__main__.A 为类A的名字。
如果将@classmethod去掉,则输出的结果为:
class A:
@staticmethod
def a():
print "this is a" # @classmethod
def b(rty):
print(rty)
print "this is b" testa = A()
testa.a()
testa.b() # 输出为:
# this is a
# <__main__.A instance at 0x1070f03f8>
# this is b
可以看到这个时候rty的内容为,类A实例的地址。它和c++的类方法不是一回事儿,c++,java的类方法必须通过实例来调用。
python 中 staticmethod 和 classmethod有什么区别的更多相关文章
- 面试题:python 中 staticmethod 和 classmethod有什么区别
面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...
- 基于python中staticmethod和classmethod的区别(详解)
例子 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 class A(object): def foo(self,x): print "executing foo ...
- python(三)@staticmethod和@classmethod使用和区别
转载自[1] 一般要用某个类的方法,先要实例这个类. 但是可以通过@staticmethod和@classmethod,直接用“类.方法()”来调用这个方法. 而 @staticmethod和@cla ...
- python中 staticmethod与classmethod区别
staticmethod与classmethod区别 参考 https://stackoverflow.com/questions/136097/what-is-the-difference-betw ...
- Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数 区别(完全解析)
原文地址:http://blog.csdn.net/caroline_wendy/article/details/23383995 还有一篇:http://blog.csdn.net/carolzha ...
- python中@staticmethod、@classmethod和实例方法
1.形式上的异同点: 在形式上,Python中:实例方法必须有self,类方法用@classmethod装饰必须有cls,静态方法用@staticmethod装饰不必加cls或self,如下代码所示: ...
- python中@staticmethod与@classmethod
@ 首先这里介绍一下‘@’的作用,‘@’用作函数的修饰符,是python2.4新增的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.只可以对模块或者类定义的函数进行修饰,不允许修饰一个 ...
- python中 staticmethod与classmethod
原文地址https://blog.csdn.net/youngbit007/article/details/68957848 原文地址https://blog.csdn.net/weixin_3565 ...
- Python中@staticmethod和@classmethod的作用和区别
简单介绍一下两者的区别: 对于一般的函数test(x),它跟类和类的实例没有任何关系,直接调用test(x)即可 #!/usr/bin/python # -*- coding:utf-8 -*- de ...
随机推荐
- 去除chrome网站https的安全检测
chrome://net-internals/#hsts 访问该网址,把要禁止检测的网址放在下面:
- AJAX学习笔记——跨域
跨域 一个域名地址的组成 http:// www abc.com : 8080 / scripts/jquery.js 协议 子域名 主域名 端口号 请求资源地址 端口号:一般来说域名端口号是80,如 ...
- C#格式化信息,格式化数字、格式化日期
一.格式化方法: 1.ToString()实例方法 使用当前文化: varname.ToString("C4"); 使用特定文化: varname.ToString("C ...
- Java中String、StringBuffer、StringBuilder、StringTokenizer的区别
Java语言中,有4个类可以对字符或字符串进行操作,它们是Character.String.StringBuffer.StringTokenizer,其中Character用于单个字符操作,Strin ...
- modbus字符串的结束符介绍
Modbus协议是应用于电子控制器上的一种通用语言.通过此协议,控制器相互之间.控制器经由网络(例如以太网)和其它设备之间可以通信.它已经成为一通用工业标准.有了它,不同厂商生产的控制设备可以连成工业 ...
- BZOJ3331 压力 (圆方树+树上差分)
题意 略 题解 求路径上的割点. 然后就直接圆方树上差分 CODE #include <bits/stdc++.h> using namespace std; inline void rd ...
- Java Executor框架使用
Java Executor框架是Jdk1.5之后推出的,是为了更加方便的开发多线程应用而封装的框架: 相比传统的Thread类,Java Executor使用方便,性能更好,更易于管理,而且支持线程池 ...
- python 操作excle 之第三方库 openpyxl学习
目录 python 操作excle 之第三方库 openpyxl学习 安装 pip install openpyxl 英文文档链接 : 点击这里~ 1,定位excel 2,读取excle中的内容 3, ...
- [Dart] splitMapJoin
var str3 = '''Multi Line String'''; print( str3.splitMapJoin( RegExp(r'^', multiLine: true), // Matc ...
- 使用jQuery快速高效制作网页交互特效---使用jQuery操作DOM
DOM操作分类 1.DOM Core:任何一种支持DOM的编程语言都可以使用它,如getElementById() 2:HTML-DOM:用于处理HTML文档,如document.forms 3:CS ...