面试题: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的类方法不同。类方法的参数是一个类,实例方法的参数是一个实例。
PS:遇到问题没人解答?需要Python学习资料?可以加点击下方链接自行获取
note.youdao.com/noteshare?id=2dce86d0c2588ae7c0a88bee34324d76
例如下面的程序:
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和实例方法
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区别
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的作用和区别
简单介绍一下两者的区别: 对于一般的函数test(x),它跟类和类的实例没有任何关系,直接调用test(x)即可 #!/usr/bin/python # -*- coding:utf-8 -*- de ...
随机推荐
- 格式化字符串漏洞 format string exploit(一)
本文系原创,转载请说明出处 本文为基于CTF WIKI的PWN学习 0x00 格式化字符串原理 先附一张经典的图,如下 其栈上布局如下: some value 3.14 123456 addr of ...
- Android 插件化开发(三):资源插件化
在前面的文章中我们成功的加载了外部的Dex(Apk)并执行了插件的Bean代码.这时我们会想,能不能加载并运行插件Apk的Activity.答案当然是能,否则后续我们的研究就没意义了,但是想实现Act ...
- kotlin之变量的可空与非空
版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/218 kotlin之变量的可空与非空 上面一篇文章,介绍了 ...
- birt fatal error致命异常错误
- nfs 所有的版本的 RFC 整理; nfs 所有版本对比;
下面是针对 nfs 所有的版本,我们可以通过不同的RFC 进行详细看其RFC的细节来进行对比: 下面是备忘一些NFS RFC 的链接: https://datatracker.ietf.org/doc ...
- Markdown的常用方法总结
1.标题 # 大标题 ## 副标题 ### 小标题 标准 2.强调 *斜体类型* **黑体字** 3.折叠 折叠长句 <details><summary>Boostnote是对 ...
- java开发两三事(2)-java多数据源+java8stream与LocalDateTime时间差
1. 场景描述 最近在工作中碰到的几个问题,有点坑,记录下,遇到相同或类似问题的朋友可以参考下. 2. 解决方案 2.1 拼接sql后,多数据源执行 采用Spring+DruidDataSource数 ...
- jumpserver 资产管理及授权
1.用户管理-添加[用户列表] 1.1点击创建用户 1.2创建用户 2.用户管理-添加[用户组] 2.1点击创建用户组 2.2创建用户组 3.资产管理添加资产 3.1添加节点 3.2添加资产(点击 ...
- Java基础语法02-流程控制-if-switch-for-while
流程控制语句 顺序结构 任何编程语言中最常见的程序结构就是顺序结构.顺序结构就是程序从上到下逐行地执行,中间没有任何判断和跳转. 分支结构 if(条件表达式){ 语句体;} 执行流程 首先判断条件表达 ...
- Spring cloud Feign 深度学习与应用
简介 Spring Cloud Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单.Feign提供了HTTP请求的模板,通过编写简单的接口和插入注解 ...