Python
中文处理
- 截取中文字符串 参考了limodou老大的做法,我这个在django中使用。不知道为什么,在django中len('中文')=6
Python AOP
- 用原类实现AOP
1 class MetaAll(type): 2 def __new__(mcl, name, bases, dict): 3 def log(func): 4 def f(*args, **kwds): 5 print 'pre %s' % func.__name__ 6 value = func(*args, **kwds) 7 print 'post %s' % func.__name__ 8 return value 9 return f 10 for attr, value in dict.iteritems(): 11 if attr.startswith('set'): 12 dict[attr] = log(value) 13 return super(MetaAll, mcl).__new__(mcl, name, bases, dict) 14 15 __metaclass__ = MetaAll 16 17 class A: 18 def __init__(self): 19 self.__x = 1 20 21 def getx(self): 22 return self.__x 23 24 def setx(self, value): 25 self.__x = value 26 27 a = A() 28 a.setx(2) 29 print a.getx()