Differences between revisions 1 and 2
Revision 1 as of 2006-07-26 05:26:01
Size: 768
Editor: HoLin
Comment:
Revision 2 as of 2006-07-26 05:26:18
Size: 768
Editor: HoLin
Comment:
Deletions are marked like this. Additions are marked like this.
Line 4: Line 4:
  {{{!#python   {{{#!python

Python

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()
      

HoLin/Python (last edited 2009-12-25 07:16:42 by localhost)