Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 0.54 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # see also https://gist.github.com/1093323
  2. class Foo(object):
  3.     baz = 15
  4.     def deco(f):
  5.         '''an in-class decorator method'''
  6.         def fun(self, *args, **kwargs):
  7.             '''use python generic reference to arguments and implicit self'''
  8.             print "before. baz is %s" % self.baz
  9.             try:
  10.                 return f(self, *args, **kwargs)
  11.             finally:
  12.                 print "after. baz is %s" % self.baz
  13.         return fun
  14.     @deco
  15.     def bar(self):
  16.         print "in bar, mutating baz"
  17.         self.baz = self.baz * 2
  18.  
  19. Foo().bar()