Chinaunix首页 | 论坛 | 博客
  • 博客访问: 61914
  • 博文数量: 17
  • 博客积分: 25
  • 博客等级: 民兵
  • 技术积分: 220
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-07 11:14
个人简介

。roth lower(substr(ename,2,length(ena)))

文章分类

全部博文(17)

文章存档

2017年(14)

2013年(3)

我的朋友

分类: Python/Ruby

2017-03-26 21:53:49


1,装饰器带类参数

点击(此处)折叠或打开

  1. # -*- coding:utf-8 -*-
  2. '''''示例8: 装饰器带类参数'''

  3. class locker:
  4.     def __init__(self):
  5.         print("locker.__init__() should be not called.")

  6.     @staticmethod
  7.     def acquire():
  8.         print("locker.acquire() called.(这是静态方法)")

  9.     @staticmethod
  10.     def release():
  11.         print(" locker.release() called.(不需要对象实例)")

  12. def deco(cls):
  13.     '''''cls 必须实现acquire和release静态方法'''
  14.     def _deco(func):
  15.         def __deco():
  16.             print("before %s called [%s]." % (func.__name__, cls))
  17.             cls.acquire()
  18.             try:
  19.                 return func()
  20.             finally:
  21.                 cls.release()
  22.         return __deco
  23.     return _deco

  24. @deco(locker)
  25. def myfunc():
  26.     print(" myfunc() called.")

  27. myfunc()
  28. myfunc()

2,类中定义和调用

点击(此处)折叠或打开

  1. import math
  2. import time
  3. from functools import wraps



  4. def deco(func):
  5.     print "Enter deco"
  6.     def wrapper(*args, **kwargs):
  7.         print "Enter deco wrapper"
  8.         start = time.time()
  9.         func(*args, **kwargs)
  10.         end = time.time()
  11.         msecs = (end - start)*1000
  12.         print "-->:%s ms" % msecs
  13.     return wrapper


  14. class MathFuc:

  15.     __X = 1

  16.     def jia(self,x,y):
  17.         return x + y

  18.     def cheng(self,x ,y):
  19.         return x * y

  20.     def testFuc(self,x, o ,y):
  21.         operate = {'+':self.jia,'*':self.cheng}

  22.         print operate.get(o)(x, y)
  23.         print 'operate[o](x, y):',operate[o](x, y)
  24.         print '__X is old', self.__X


  25.     @deco
  26.     def myfunc(self,a,b):
  27.         print "start func"
  28.         time.sleep(0.8)
  29.         print "end func"
  30.         print "rest is %s"%(a+b)


  31.     @classmethod
  32.     def tearDownClass(self):
  33.         MathFuc.__X = 10
  34.         print 'MathFuc Down X is ', MathFuc.__X

  35.     @staticmethod
  36.     def staticClass():
  37.         MathFuc.__X = 20
  38.         print 'static MathFuc X is ', MathFuc.__X


  39. if __name__ =='__main__':

  40.     fun = MathFuc()

  41.     fun.myfunc(6, 7)

  42.     print fun.myfunc.__name__




阅读(597) | 评论(0) | 转发(0) |
0

上一篇:linux man for awk

下一篇:几种执行shell的方法

给主人留下些什么吧!~~