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)和实例方法的更多相关文章

  1. 静态方法staticmethod类方法classmethod

    静态方法 只是名义上归类管理,实际上在静态方法里访问不了类或者实例中的任何属性. 类方法 只能访问类变量,不能访问实例变量 属性方法 把一个方法变成一个静态属性,调用的时候不能加() 如果这种属性方法 ...

  2. python学习日记(OOP——静态方法和类方法)

    classmethod 类方法在Python中使用比较少,类方法传入的第一个参数为cls,是类本身.并且,类方法可以通过类直接调用,或通过实例直接调用.但无论哪种调用方式,最左侧传入的参数一定是类本身 ...

  3. Python语言特性之3:@staticmethod和@classmethod

    问题:Python中@staticmethod和@classmethod两种装饰器装饰的函数有什么不同? 原地址:http://stackoverflow.com/questions/136097/w ...

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

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

  5. python面向对象-3类的静态方法和类方法

    还是以上次的洗衣机例子: class Washer: company='ZBL' def __init__(self,water=10,scour=2): self._water=water #不想让 ...

  6. 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  ...

  7. python中 staticmethod与classmethod

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

  8. 【Python】@staticmethod和@classmethod的作用与区别

    前言 Python其实有3个方法,即静态方法(staticmethod),类方法(classmethod)和实例方法,一般来说,要使用某个类的方法,需要先实例化一个对象再调用方法.而使用@static ...

  9. Python类三种方法,函数传参,类与实例变量(一)

    1 Python的函数传递: 首先所有的变量都可以理解为内存中一个对象的'引用' a = 1 def func(a): a = 2 func(a) print(a) # 1 a = 1 def fun ...

随机推荐

  1. error while loading shared libraries错误解决

    在编译引用了第三方库的代码后,执行出现了以下错误 [work@xxx zktest]$ ./a.out ./a.out: error while loading shared libraries: l ...

  2. 380. Intersection of Two Linked Lists【medium】

    Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...

  3. h5-文本框

    h5-文本框 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  4. layui点击table表格的每一格时显示相应的内容

    $(document).on('click','.layui-table-cell',function(){ // $("p").css({"background-col ...

  5. SQLi-Labs学习笔记

    结构化查询语言,也叫做SQL,从根本上说是一种处理数据库的编程语言.对于初学者,数据库仅仅是在客户端和服务端进行数据存储.SQL通过结构化查询,关系,面向对象编程等等来管理数据库.编程极客们总是搞出许 ...

  6. css断句 word-break

    word-break:break-all;word-wrap:break-word; 兼容IE6 火狐 chrome

  7. Uploadify使用源码

    上传图片页面绑定源码如下: $("#uploadify").uploadify({ 'uploader' : basePath+'commons/uploadfiles/uploa ...

  8. PAT006 Tree Traversals Again

    题目: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For exa ...

  9. iOS conformsToProtocol

    - (BOOL)conformsToProtocol:(Protocol *)aProtocol; 是用来检查对象(包括其祖先)是否实现了指定协议类的方法. 今天遇到一个问题,一个类没有实现proto ...

  10. word中使用MathType能做什么

    在Office中写论文,特别是一些比较专业的论文需要用到各种公式的.会发现有很多地方Office自带的公式编辑器都无法完成,所以要用到MathType公式编辑器这个好用的工具了.MathType是一款 ...