面试中经常会问到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. jcmd命令实战

    继续来根据之前的那篇infoq的文章的介绍熟悉工具,上一次咱们学习使用了: 接下来学习它里面提到的另一个工具: jcmd是一个非常之强大的命令行工具,能输出很多很多的信息,也是在处理JVM的一些问题经 ...

  2. Jenkins构建自动化--实例一

    1.新建任务 2.配置任务 3.构建任务 5.构建完成后,可以点击任务名称查看结果

  3. 电脑视频下载王-Apowersoft Video Download Capture v6.3.6

    Apowersoft Video Download Capture (视频下载王) 是由香港Apowersoft出品的一款集视频下载.视频转换.媒体播放及录屏等功能为一体的多功能视频下载工具,简便实用 ...

  4. tcp文件下载

    服务器端 import socket def send_file_2_client(new_client_socket,client_addr): # 接收信息 file_name = new_cli ...

  5. Selenium常用API的使用java语言之3-selenium3 浏览器驱动

    1.下载浏览器驱动 当selenium升级到3.0之后,对不同的浏览器驱动进行了规范.如果想使用selenium驱动不同的浏览器,必须单独下载并设置不同的浏览器驱动. 各浏览器下载地址: Firefo ...

  6. python func(*args, **kwargs)

    func(*args, **kwargs) *args, **kwargs表示函数的可变参数 *args 表示任何多个无名参数,它是一个tuple **kwargs 表示关键字参数,它是一个dict ...

  7. 引入其他服务器的JS文件,同时也引入本地JS文件 报错时:

    控制台报错: Uncaught ReferenceError: define is not defined at core.js:5

  8. Java Part 001( 03_01_数据类型和运算符 )

    注释 Java语言的注释一共有三种类型,分别是单行注释.多行注释和文档注释. 1. 单行注释 单行注释就是在程序中注释一行代码,在Java语言中,使用双斜线“//”进行单行注释. 2. 多行注释 多行 ...

  9. oracle查询数据库连接数相关

    select username,count(username) from v$session where username is not null group by username;--查询各个用户 ...

  10. .NET项目 - sqlserver 连接字符串

    服务器本地连接: <connectionStrings> <add name="db" connectionString="Data Source=lo ...