面试中经常会问到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有什么区别的更多相关文章

  1. 面试题:python 中 staticmethod 和 classmethod有什么区别

    面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...

  2. 基于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 ...

  3. python(三)@staticmethod和@classmethod使用和区别

    转载自[1] 一般要用某个类的方法,先要实例这个类. 但是可以通过@staticmethod和@classmethod,直接用“类.方法()”来调用这个方法. 而 @staticmethod和@cla ...

  4. python中 staticmethod与classmethod区别

    staticmethod与classmethod区别 参考 https://stackoverflow.com/questions/136097/what-is-the-difference-betw ...

  5. Python - 静态函数(staticmethod), 类函数(classmethod), 成员函数 区别(完全解析)

    原文地址:http://blog.csdn.net/caroline_wendy/article/details/23383995 还有一篇:http://blog.csdn.net/carolzha ...

  6. python中@staticmethod、@classmethod和实例方法

    1.形式上的异同点: 在形式上,Python中:实例方法必须有self,类方法用@classmethod装饰必须有cls,静态方法用@staticmethod装饰不必加cls或self,如下代码所示: ...

  7. python中@staticmethod与@classmethod

    @ 首先这里介绍一下‘@’的作用,‘@’用作函数的修饰符,是python2.4新增的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.只可以对模块或者类定义的函数进行修饰,不允许修饰一个 ...

  8. python中 staticmethod与classmethod

    原文地址https://blog.csdn.net/youngbit007/article/details/68957848 原文地址https://blog.csdn.net/weixin_3565 ...

  9. Python中@staticmethod和@classmethod的作用和区别

    简单介绍一下两者的区别: 对于一般的函数test(x),它跟类和类的实例没有任何关系,直接调用test(x)即可 #!/usr/bin/python # -*- coding:utf-8 -*- de ...

随机推荐

  1. P1967 货车运输[生成树+LCA]

    题目描述 A国有n座城市,编号从 1到n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q* 辆货车在运输货物, 司机们想知道每辆车在不超过车辆限重的情况下,最多能运多重 ...

  2. 使用poi进行数据的导出Demo

    这是本人在项目中遇到了一个导出数据时,如果该条数据中包含汉字,就会出现excel单元格的大小与期望的样式不一样,也是查找了半天,也没有发现哪里出的问题. 现将一个小Demo奉献在这里,可以在遇到使用p ...

  3. .NET Core 开发常用命令(VS Code)

    在开始开发 .NET Core 项目的时候,有用过 VS2017.VS Code 两个对比下来,VS 虽然开发更便捷但是 VS Code 更适合 .NET Core. 下面就总结一下常用的命令. 一. ...

  4. XAMPP启动Apache服务时启动失败

    9:48:49 [Apache] Apache Service detected with wrong path 9:48:49 [Apache] Change XAMPP Apache and Co ...

  5. 负载均衡集群(LBC)

    一.LVS简介及工作模式1. LVS简介Linux Virtual Server,该软件的功能是实现LB(load balance) 2.LVS的三种工作模式 1)NAT模式(NAT) LVS 服务器 ...

  6. python - django (母版与组件)

    # 把多个页面通用的部分提取出来 放在一个母版中 # 其它的页面只需要继承 母版就可以 # 使用步骤:( 继承的语句要放在第一行 ) """ 1. 把公用的 HTML 部 ...

  7. vue app.xxx.js 较大问题

    线上build之后发现app.XXX.js 文件特别大. 包我都改为cdn了 其他空间就是路由改为懒加载了. { path: '/a/b', name: 'ab', component: () =&g ...

  8. 安装pip的三种方式

    pip是python的一个工具,用来安装python包特别方便.Linux系统是是内置python程序,因为许多Linux内置文件都是使用python来编写的,比如说yum. 1.脚本安装 通过脚本的 ...

  9. spark的广播变量

    直接上代码:包含了,map,filter,persist,mapPartitions等函数 String master = "spark://192.168.2.279:7077" ...

  10. $noip2018$游记+考后总结

    游记部分 Day-5 - Day0 敲了敲模板,打了几场模拟赛,都在颓废,其他什么都没做 Day1 早上继续写模板,水了会QQ,其他什么都没做 中午试图背模板,没成功(其实下午才发现敲的模板一个没用上 ...