方法python
绑定方法和非绑定方法ssh
绑定方法和非绑定方法在建立时没有任何区别,同一方法,既能够为绑定方法,也能够为非绑定方法,一切不一样都只在调用时的手法上有所区别。函数
绑定方法即该方法绑定类的一个实例上,必须将self做为第一个参数传入,而这个过程是由Python自动完成。能够经过实例名.方法名(参数列表)来调用。spa
非绑定方法由于不绑定到实例上,因此在引用时经过类来进行引用。该过程不是 Python 自动完成,若是忘记传入实例,那么直接调用是确定会出问题的。因此要调用类的非绑定方法时应该显示地提供一个实例做为第一个参数。使用类名.非绑定方法名(参数列表)
的形式来进行引用code
1 >>> class Foo(object): 2 def bar(self): 3 print "normal class" 4 5 6 >>> f =Foo() 7 >>> f.bar() #实例方法调用,不用显示的传入第一个参数self. 8 normal class 9 >>> Foo.bar(f) #类名.方法名 必须传入参数f 10 normal class 11 >>> Foo.bar 12 <unbound method Foo.bar> #非绑定方法的对象 13 >>> f.bar #绑定方法 14 <bound method Foo.bar of <__main__.Foo object at 0x0000000003370B00>> 15 >>> dir(Foo)#查看类的属性的名称 16 ['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'bar'] 17 >>> Foo.__dict__ #特殊属性__dict__,做用查看类的属性,是字典形式,key是属性的名称,value相应属性对象的数据值 18 dict_proxy({'__dict__': <attribute '__dict__' of 'Foo' objects>, '__weakref__': <attribute '__weakref__' of 'Foo' objects>, '__module__': '__main__', 'bar': <function bar at 0x0000000003FC5EB8>, '__doc__': None}) 19 >>> Foo.__dict__['bar'] #查看bar是一个函数对象 20 <function bar at 0x0000000003FC5EB8>
#desr.__get__(self,obj,type=None)
#get、set、 del有人称为黑魔法,咱们叫它描述器
21 >>> Foo.__dict__['bar'].__get__(None,Foo) #第一个参数self赋值为None,意思是没有给定的实例,非绑定方法 22 <unbound method Foo.bar> #非绑定方法 23 >>> f.bar #绑定方法 24 <bound method Foo.bar of <__main__.Foo object at 0x0000000003370B00>> 25 >>> Foo.bar #非绑定方法 26 <unbound method Foo.bar> 27 >>> Foo.__dict__['bar'].__get__(f,Foo) #第一个参数self 给定一个实例,绑定方法 28 <bound method Foo.bar of <__main__.Foo object at 0x0000000003370B00>> 29 >>>
#小结:经过类来获取方法的时候,获得的是非绑定方法对象;经过实例来获取方法的时候,获得的是绑定方法对象
静态方法和类方法orm
语法上面的区别:对象
使用的区别:blog
因为静态方法没法访问类属性,实例属性,至关于一个相对独立的方法,跟类其实并无什么关系。这样说来,静态方法就是在类的做用域里的函数而已。utf-8
类方法:实例方法相似,不过其第一个参数通常是 cls (约定俗成)而不是 self。可是,若是咱们直接将 self 换成 cls 来建立类方法是不对的,由于实例方法的首个参数也是任意的,只是统一使用 self 。python的解释器并无说看见第一个参数是 cls 就知道这个是类方法,它仍是将其看成是实例方法来对待,因此咱们须要经过内建函数: classmethod() 来建立类方法。作用域
示例:类方法
1 #! /usr/bin/env python 2 #coding:utf-8 3 4 class Foo(object): 5 6 one = 0 7 8 def __init__(self): #初始化函数 9 Foo.one =Foo.one + 1 #类属性+1 10 11 @classmethod #建立类方法 12 def get_class_attr(cls):#传递一个参数 13 return cls.one #参数对象的属性one 14 15 16 if __name__ =="__main__": 17 f1 =Foo() 18 print "f1:",Foo.one 19 f2 =Foo() 20 print "f2:",Foo.one 21 22 print f1.get_class_attr() 23 print "f1.one:",f1.one 24 print Foo.get_class_attr() 25 26 print "*"*10 27 f1.one = 8 #实例one属性 28 Foo.one = 9 #类的one属性 29 print f1.one 30 print f1.get_class_attr() 31 print Foo.get_class_attr() 32 33 34 #output 35 #f1: 1 36 #f2: 2 37 #2 38 #f1.one: 2 39 #2 40 #********** 41 #8 42 #9 43 #9
#小结:类方法的定义其实就是在方法前面要写上@classmethod,在定义方法时,跟通常方法同样,参数采用cls,目的是经过cls把类这个对象传入
静态方法
静态方法:就是类中的一个普通函数,它并无默认传递的参数,在建立静态方法的时候,须要用到内置函数: staticmethod() 。
1 #! /usr/bin/env python 2 #coding:utf-8 3 4 T =1 5 6 class Foo(object): 7 8 def __init__(self,name): 9 self.name = name 10 11 @staticmethod 12 def check_t(): 13 T =1 14 return T 15 16 def get_name(self): 17 if self.check_t(): 18 return self.name 19 else: 20 return "no person" 21 22 if __name__ == "__main__": 23 24 f = Foo("cc") 25 name =f.get_name() 26 print name