python中 staticmethod与classmethod区别
staticmethod与classmethod区别
参考
- https://stackoverflow.com/questions/136097/what-is-the-difference-between-staticmethod-and-classmethod-in-python
- http://blog.csdn.net/handsomekang/article/details/9615239
例子
class A(object):
def foo(self,x):
print "executing foo(%s,%s)"%(self,x)
@classmethod
def class_foo(cls,x):
print "executing class_foo(%s,%s)"%(cls,x)
@staticmethod
def static_foo(x):
print "executing static_foo(%s)"%x
a=A()
上述类有三个函数,使用如下:
a.foo(1)
# executing foo(<__main__.A object at 0xb7dbef0c>,1)
-----------------------------------------------------------------
a.class_foo(1)
# executing class_foo(<class '__main__.A'>,1)
A.class_foo(1)
# executing class_foo(<class '__main__.A'>,1)
-----------------------------------------------------------------
a.static_foo(1)
# executing static_foo(1)
A.static_foo('hi')
# executing static_foo(hi)
区别
foo()
的调用者必须是类A
的一个实例,class_foo()
与static_foo()
的调用者既可以是类也可以是某个实例参数不同,
foo()
参数为self和其他参数,class_foo()
参数使用类(cls)替换了self,static_foo()
则只有参数,没有self和类(cls)a.foo(1)
中的foo()
与a是绑定的,class_foo()
是与类绑定的,而static_foo()
与这两者都没有绑定,可以使用print
来查看,如下:print(a.foo)
# <bound method A.foo of <__main__.A object at 0xb7d52f0c>> print(a.class_foo)
# <bound method type.class_foo of <class '__main__.A'>> print A.class_foo
<bound method classobj.class_foo of <class __main__.A at 0x105efedb8>> print(a.static_foo)
# <function static_foo at 0xb7d479cc>
print(A.static_foo)
# <function static_foo at 0xb7d479cc>
作用
- 使用场景:classmethod在一些工厂类的情况下使用较多,也就是说OOP里继承的时候使用,staticmethod一般情况下可以替换为外部的函数,后者继承的时候不可更改,和C++/JAVA中的静态方法很相似
- 有利于组织代码,同时有利于命名空间的整洁
python中 staticmethod与classmethod区别的更多相关文章
- 基于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有什么区别
面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...
- python 中 staticmethod 和 classmethod有什么区别
面试中经常会问到staticmethod 和 classmethod有什么区别? 首先看下官方的解释: staticmethod: class staticmethod staticmethod(fu ...
- 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 ...
- python中// 和/有什么区别
python中// 和/有什么区别 通常C/C++中,"/ " 算术运算符的计算结果是根据参与运算的两边的数据决定的,比如: 6 / 3 = 2 ; 6,3都是整数,那么结果也就是 ...
- Python中__repr__和__str__区别
Python中__repr__和__str__区别 看下面的例子就明白了 class Test(object): def __init__(self, value='hello, world!'): ...
随机推荐
- Java中用HttpsURLConnection访问Https链接
在web应用交互过程中,有很多场景需要保证通信数据的安全:在前面也有好多篇文章介绍了在Web Service调用过程中用WS-Security来保证接口交互过程的安全性,值得注意的是,该种方式基于的传 ...
- ROS学习(十三)—— 编写简单的Service和Client (C++)
一.编写Service节点 1.节点功能: 我们将创建一个简单的service节点("add_two_ints_server"),该节点将接收到两个整形数字,并返回它们的和. 2. ...
- Linux下通过关键字模糊查找搜索文件
[背景] 想要在Linux下面,找之前不知道放到哪里的一个tomcat的文件. [折腾过程] 1.最后是参考: linux查找文件命令find – 发芽的石头 – 博客频道 – CSDN.NET 去搜 ...
- log4j(四)——如何控制不同风格的日志信息的输出?
一:测试环境与log4j(一)——为什么要使用log4j?一样,这里不再重述 二:老规矩,先来个栗子,然后再聊聊感受 import org.apache.log4j.*; //by godtrue p ...
- MongoDB 学习笔记(8)---$type 操作符
$type操作符是基于BSON类型来检索集合中匹配的数据类型,并返回结果. MongoDB 中可以使用的类型如下表所示: 类型 数字 备注 Double 1 String 2 Object 3 ...
- linux\mac 日常入门命令行使用——搜索文件\文件夹
搜索文件或者文件夹,是一个常见的需求.我们可以用多种命令来实现我们的需求. find 命令实现搜索 find 是英文,寻找的意思.这个命令可以很方面的来搜索我们需要的内容. 标准命令如下: find ...
- org.hibernate.exception.ConstraintViolationException: could not insert:
org.hibernate.exception.ConstraintViolationException: could not insert: 报错原由于xxx.hbm.xml文件里的主键类型设置有问 ...
- [转]OkHttp3 最有营养的初级教程
一.前言 自从Android4.4开始,google已经开始将源码中的HttpURLConnection替换为OkHttp,而在Android6.0之后的SDK中google更是移除了对于HttpCl ...
- Four Node.js Gotchas that Operations Teams Should Know about
There is no doubt that Node.js is one of the fastest growing platforms today. It can be found at sta ...
- SQL Server 自增字段归零
方法一: 如果曾经的数据都不需要的话,可以直接清空所有数据,并将自增字段恢复从1开始计数 truncate table 表名 方法二: DBCC CHECKIDENT (''table_name'', ...