Python和c++一样,可以界说类,可以担任,类中又包括了类变量、实例变量(私有变量和公有变量)、要领(包罗静态要领staticmethod、类要领classmethod和实例要领instancemethod)。这里只着重先容类的成员。
通过语言描写会较量费事,下面通过例子来说明
# coding: utf-8 class MyClass: '''I simple example class''' val1 = 'Value 1' #类变量 val4 = 1 def __init__(self): self.val2 = 'Value 2' #公有实例变量 self.__val3 = 'Value 3' #私有实例变量 def __func(): print 'val1 : ', MyClass.val1 print 'static method cannot access val2' print 'static method cannot access __val3' print 'val4 : ', MyClass.val4 MyClass.val4 = ((MyClass.val4 + 1)) smd = staticmethod(__func) def __func2(cls): print 'val1 : ', cls.val1 print 'class method cannot access val2' print 'class method cannot access __val3' print 'val4 : ', cls.val4 cls.val4 = ((cls.val4 + 1)) cmd = classmethod(__func2) def func3(self): print 'val1 : ', self.val1 print 'val2 : ', self.val2 print 'instance method cannot access __val3' print 'val4 : ', self.val4 self.val4 = ((self.val4 + 1))
这个类中已经根基包括了上面提到的类中的各类成员,然后通过挪用看下这些成员有什么差异
print '--------------------MyClass.smd()-------------------' MyClass.smd() #类挪用静态要领 print '--------------------MyClass.cmd()-------------------' MyClass.cmd() #类挪用类要领 #MyClass.func3() #类无法直接挪用实例要领 x = MyClass() print '--------------------x.smd()-------------------' x.smd() #实例挪用静态要领 print '--------------------x.cmd()-------------------' x.cmd() #实例挪用类要领 print '--------------------x.func3()-------------------' x.func3() #实例挪用实例要领 #功效 --------------------MyClass.smd()------------------- val1 : Value 1 static method cannot access val2 static method cannot access __val3 val4 : 1 --------------------MyClass.cmd()------------------- val1 : Value 1 class method cannot access val2 class method cannot access __val3 val4 : 2 --------------------x.smd()------------------- val1 : Value 1 static method cannot access val2 static method cannot access __val3 val4 : 3 --------------------x.cmd()------------------- val1 : Value 1 class method cannot access val2 class method cannot access __val3 val4 : 4 --------------------x.func3()------------------- val1 : Value 1 val2 : Value 2 instance method cannot access __val3 val4 : 5
团结上面的输出功效来表明下差异变量和要领的成果。
静态要领,可以认为是一种全局要领,因为它不需要类实例化就能会见,和模块内的要领没什么区别,可以通过类和实例举办挪用,它不能会见实例变量。虽然,但可以或许通过类名会见类变量,如MyClass.val1。
类要领,雷同是个全局要领,它也能如静态要领那样被类挪用,也能被实例挪用,差异的是它通过实例来会见类变量,有类变量cls传入,而且有子类担任时,挪用该类要领时,传入的类变量cls是子类,而非父类,如x.val1。
实例要领,实例要领只能通过实例会见,它可以或许会见实例变量(公有)和类变量。
私有要领,无法被类和实例挪用。
类变量,可以或许被类、类要领、实例和实例要领等会见。且在类和实例中举办通报(不断累加),如val4。
实例变量(公有),能被实例和实例要了解见,但不能被类和类要了解见。
实例变量(私有),不能被任何实例会见,但我们可以通过装饰器对其增加get/set要领来举办操纵,详细在下面先容。
私有属性,通过在变量和要领前增加__(两个下划线)来界说。
装饰器
简朴的说,装饰器就是对函数的一种装饰,可以在不修改被装饰函数界说和挪用的环境下,增加对被挪用函数的操纵或指定其属性。
语法糖
通过装饰器简化要领装饰的代码
# coding: utf-8 def deco(func): def _deco(): print("before myfunc() called.") func() print(" after myfunc() called.") return _deco @deco def myfunc(): print(" myfunc() called.") return 'ok' myfunc() myfunc() #输出 before myfunc() called. myfunc() called. after myfunc() called. before myfunc() called. myfunc() called. after myfunc() called.
多层装饰器
@A @B @C def func(): ...
可以当作是A(B(C(func))),依次执行A、B、C和func。
内置装饰器(将上面的MyClass类从头用装饰器界说一次,并增加对私有变量的操纵要领)
# coding: utf-8 class MyClass: '''I simple example class''' val1 = 'Value 1' val4 = 1 def __init__(self): self.val2 = 'Value 2' self.__val3 = 'Value 3' def func3(self): #界说实例要领 print 'val1 : ', self.val1 print 'val2 : ', self.val2 print 'instance method cannot access __val3' print 'val4 : ', self.val4 self.val4 = ((self.val4 + 1)) @classmethod #界说类要领 def func2(cls): print 'val1 : ', cls.val1 print 'class method cannot access val2' print 'class method cannot access __val3' print 'val4 : ', cls.val4 cls.val4 = ((cls.val4 + 1)) @staticmethod #界说静态要领 def func(): print 'val1 : ', MyClass.val1 print 'static cannot access val2' print 'static method cannot access __val3' print 'val4 : ', MyClass.val4 MyClass.val4 = ((MyClass.val4 + 1)) @property #私有实例变量get属性 def val3(self): return self.__val3 @val3.setter #私有实例变量set属性 def val3(self, value): self.__val3 = value @val3.deleter #私有实例变量del属性 def val3(self): del self.__val3
@classmethod和@staticmethod别离取代了之前的类要领和静态要领的声明方法,除了简捷外没有其它非凡的意义。
#p#分页标题#e#
在2中界说的类中,有一个私有实例变量,它不能被类和实例所会见,我们通过增加@property和@setter来使实例可以或许对私有变量举办会见和赋值,并可以通过@deleter来将该变量删除。留意,类也能通过@property举办私有变量的会见,但无法通过@setter来给私有变量赋值,且@deleter不支持类会见。
挪用如下:
print '-------------------MyClass.func()------------------' MyClass.func() x = MyClass() print '-------------------x.func()------------------' x.func() print '-------------------x.func2()------------------' x.func2() print '-------------------x.func3()------------------' x.func3() print '' print 'MyClass().val3 : ',MyClass().val3 #类挪用property MyClass().val3 = 'New Value' #类挪用setter print 'after "MyClass().val3 = New Value" val3 :', MyClass().val3 print'' print 'val3 : ',x.val3 #实例挪用property x.val3 = 'New Value' #实例挪用setter print 'after "x.val3 = New Value" val3 :', x.val3 del x.val3 #实例挪用deleter print 'after "del x.val3" val3 : ', x.val3 #功效 -------------------MyClass.func()------------------ val1 : Value 1 static cannot access val2 static method cannot access __val3 val4 : 1 -------------------x.func()------------------ val1 : Value 1 static cannot access val2 static method cannot access __val3 val4 : 2 -------------------x.func2()------------------ val1 : Value 1 class method cannot access val2 class method cannot access __val3 val4 : 3 -------------------x.func3()------------------ val1 : Value 1 val2 : Value 2 instance method cannot access __val3 val4 : 4 MyClass().val3 : Value 3 #类挪用property after "MyClass().val3 = New Value" val3 : Value 3 #类挪用setter没有生效 val3 : Value 3 #实例挪用property after "x.val3 = New Value" val3 : New Value #实例挪用setter after "del x.val3" val3 : Value 3 #实例挪用deleter