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

  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和实例方法

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

  5. python中@staticmethod与@classmethod

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

  6. python中 staticmethod与classmethod

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

  7. python中 staticmethod与classmethod区别

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

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

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

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

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

随机推荐

  1. PythonI/O进阶学习笔记_8.python的可迭代对象和迭代器、迭代设计模式

     content: 1.什么是迭代协议 2. 什么是迭代器(Iterator)和可迭代对象(Iterable) 3. 使用迭代器和可迭代对象 4. 创建迭代器和可迭代对象 5. 迭代器设计模式   一 ...

  2. C语言笔记 08_函数指针&回调函数&字符串&结构体&位域

    函数指针 函数指针是指向函数的指针变量. 通常我们说的指针变量是指向一个整型.字符型或数组等变量,而函数指针是指向函数. 函数指针可以像一般函数一样,用于调用函数.传递参数. 函数指针变量的声明: / ...

  3. CVPR 2019轨迹预测竞赛冠军方法总结

    背景 CVPR 2019 是机器视觉方向最重要的学术会议,本届大会共吸引了来自全世界各地共计 5160 篇论文,共接收 1294 篇论文,投稿数量和接受数量都创下了历史新高,其中与自动驾驶相关的论文. ...

  4. 动态代理模式_应用(Redis工具类)

    本次使用动态代理的初衷是学习Redis,使用Java操作Redis时用到Jedis的JedisPool,而后对Jedis的方法进一步封装完善成为一个工具类.因为直接使用Jedis对象时,为了保证性能, ...

  5. 使用react-app-rewired和customize-cra对默认webpack自定义配置

    最近在学习react框架,之前一直都是用vue 开发,知道在vue 中知道如何配置一下相关的webpack 有助于开发,学react 过程中,我也在想这些该怎么配置啊,所以就有这篇文章. 这篇文章主要 ...

  6. Hack the Zico2 VM (CTF Challenge)

    下载链接: Download this VM here: https://download.vulnhub.com/zico/zico2.ova 端口扫描: ╰─ nmap -p1-65535 -sV ...

  7. idea上传项目到github

    1.在上传项目之前需要先在idea中确认两个配置,一个是git的执行位置,电脑上没有安装git的需要提前安装(下载git软件并且安装,非github desktop),安装之后再idea的settin ...

  8. 管网平差的python程序

    在市政给水管网当中,管网平差的目的是在已知节点流量.管段长度的情况下,求得各管段流量和对应的经济管径.本科生学习阶段了解并掌握管网平差原理及方法是必不可少的环节. 在下面的程序当中,将利用哈代克罗斯法 ...

  9. IT兄弟连 HTML5教程 使用盒子模型设计页面布局

    布局所涉及的技术非常很多,足以另写单独的一本书了.在本节中主要介绍网站中最常用的布局方案,包括区块框居中.两列浮动.三列浮动及多列浮动的区块框. 1  居中设计 区块框居中的设计是在网页布局中常用的技 ...

  10. mysql常用运行原理

    MySQL的客户端/服务器架构 MySQL的服务器程序直接和我们存储的数据打交道,然后可以有好多客户端程序连接到这个服务器程序,发送增删改查的请求,然后服务器就响应这些请求,从而操作它维护的数据.和微 ...