装饰器:
1 #装饰器 2 3 4 def outer(fun): 5 def wrapper(): 6 print ‘验证‘ 7 fun() 8 print ‘1111111‘ 9 return wrapper 10 11 @outer 12 def Func1(): 13 print ‘func1‘ 14 @outer 15 def Func2(): 16 print ‘func2‘ 17 18 Func1() 19 Func2()
装饰器1:
1 def outer(fun): 2 def wrapper(arg): 3 print ‘验证‘ 4 fun(arg) 5 print ‘1111111‘ 6 return wrapper 7 8 @outer 9 def Func1(arg): 10 print ‘func1‘,arg 11 12 Func1(‘yang‘)
类和对象:
类:
1 class Person: 2 xx = ‘12345‘ 3 4 def __init__(self,name): 5 self.name = name 6 7 p1= Person(‘李阳‘) 8 print p1.name 9 10 p2= Person(‘老苟‘) 11 print p2.name
类中的三种形式: 方法,特性,字段
1 class Province: 2 3 memo = "23shengfen" #静态字段,静态字段是属于类的 4 def __init__(self,name,capital,leade): 5 6 self.name = name 7 self.capital = capital #动态字段, 类不能访问动态字段 8 self.Leade = leade 9 10 #动态方法 11 def Sport_meet(self): 12 print self.name + ‘开运动会‘ 13 14 15 @staticmethod #静态方法 静态方法属于类 16 def Foo(): 17 print ‘每个省要带头反腐‘ 18 19 @property #特性 20 def Bar(self): 21 #print self.name 22 return ‘one‘ 23 24 hb = Province(‘河北‘,‘石家庄‘,‘杨‘) 25 sd = Province(‘山东‘,‘济南‘,‘许‘) 26 #print Province.memo 27 #print hb.name 28 #print hb.memo #对象可以访问静态字段,建议不要使用 29 30 #hb.Sport_meet() 31 #sd.Sport_meet() 32 #Province.Foo() 33 print hb.Bar #特性是这种访问形式,特性取返回值
原文:http://www.cnblogs.com/augustyang/p/6243625.html