Python的3个方法:静态方法(staticmethod),类方法(classmethod)和实例方法
Python的方法主要有3个,即静态方法(staticmethod),类方法(classmethod)和实例方法,如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
def foo(x): print "executing foo(%s)"%(x) 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() |
这个self和cls是对类或者实例的绑定,对于一般的函数来说我们可以这么调用foo(x),这个函数就是最常用的,它的工作跟任何东西(类,实例)无关.对于实例方法,我们知道在类里每次定义方法的时候都需要绑定这个实例,就是foo(self, x),为什么要这么做呢?因为实例方法的调用离不开实例,我们需要把实例自己传给函数,调用的时候是这样的a.foo(x)(其实是foo(a, x)).类方法一样,只不过它传递的是类而不是实例,A.class_foo(x).注意这里的self和cls可以替换别的参数,但是python的约定是这俩,还是不要改的好.
对于静态方法其实和普通的方法一样,不需要对谁进行绑定,唯一的区别是调用的时候需要使用a.static_foo(x)或者A.static_foo(x)来调用.
Python的3个方法:静态方法(staticmethod),类方法(classmethod)和实例方法的更多相关文章
- 静态方法staticmethod类方法classmethod
静态方法 只是名义上归类管理,实际上在静态方法里访问不了类或者实例中的任何属性. 类方法 只能访问类变量,不能访问实例变量 属性方法 把一个方法变成一个静态属性,调用的时候不能加() 如果这种属性方法 ...
- python学习日记(OOP——静态方法和类方法)
classmethod 类方法在Python中使用比较少,类方法传入的第一个参数为cls,是类本身.并且,类方法可以通过类直接调用,或通过实例直接调用.但无论哪种调用方式,最左侧传入的参数一定是类本身 ...
- Python语言特性之3:@staticmethod和@classmethod
问题:Python中@staticmethod和@classmethod两种装饰器装饰的函数有什么不同? 原地址:http://stackoverflow.com/questions/136097/w ...
- python中@staticmethod、@classmethod和实例方法
1.形式上的异同点: 在形式上,Python中:实例方法必须有self,类方法用@classmethod装饰必须有cls,静态方法用@staticmethod装饰不必加cls或self,如下代码所示: ...
- python面向对象-3类的静态方法和类方法
还是以上次的洗衣机例子: class Washer: company='ZBL' def __init__(self,water=10,scour=2): self._water=water #不想让 ...
- python-静态方法staticmethod、类方法classmethod、属性方法property
Python的方法主要有3个,即静态方法(staticmethod),类方法(classmethod)和实例方法 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 def ...
- python中 staticmethod与classmethod
原文地址https://blog.csdn.net/youngbit007/article/details/68957848 原文地址https://blog.csdn.net/weixin_3565 ...
- 【Python】@staticmethod和@classmethod的作用与区别
前言 Python其实有3个方法,即静态方法(staticmethod),类方法(classmethod)和实例方法,一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法.而使用@static ...
- Python类三种方法,函数传参,类与实例变量(一)
1 Python的函数传递: 首先所有的变量都可以理解为内存中一个对象的'引用' a = 1 def func(a): a = 2 func(a) print(a) # 1 a = 1 def fun ...
随机推荐
- iOS音频合并
iOS音频合并 最近遇到一个需求,客户录音试听一下可以,就继续向下录制,当客户录制完成后,需要把前面录制的试听音频和后面的音频进行合并.最初想到的方法,使用NSData对两个音频文件进行合并,但是合并 ...
- lua 打印 table 拷贝table
-- 打印table function print_lua_table (lua_table, indent) if lua_table == nil or type(lua_table) ~= &q ...
- python socket 多人聊天室
参考来源(其实我从上面复制了一点):Python 的 Socket 编程教程 http://www.oschina.net/question/12_76126Python线程指南 http://ww ...
- js----Navigator对象,查看浏览器信息,Screen对象,查看屏幕信息
Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本. 对象属性: 查看浏览器的名称和版本,代码如下: <script type=" ...
- SessionListener失败,退出
配置如下: web.xml: <listener> <listener-class>cn.edu.hbcf.common.listener.SessionListener< ...
- ASP.NET MVC 4新建库项目中找不到 System.Web.Security 的引用
.NET 4中,WebSecurity的引用已经不再System.Web中,而是转移到了System.Web.ApplicationServices Dll中,添加该Dll即可.
- 使用command对象添加删除查询记录
private void button1_Click(object sender, EventArgs e) { //实例化数据库连接对象 SqlConnection sqlcon = new Sql ...
- JS对象序列化为JSON对象的方法
var $ = $ || {}; /** * 将JS对象序列化为JSON字符串 * @param {Mixed} o The variable to decode * @return {String} ...
- 用position: fixed;做个遮罩,怎么能让后面的View禁止滑动
用一个view标签把代码包起来,当模态层出来时给它添加height:100%;position: absolute;overflow: hidden;.模态框消失时去掉样式
- thinkphp nginx 上配置 并解决get获取到数据现象
server { listen 80; server_name XXXX.funova.net XXX.funova.com; root /opt/newgm; index index.php; lo ...