Mercurial > hg > config
comparison python/example/bind.py @ 526:4063bbeaa7d4
initial commit
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Tue, 24 Sep 2013 13:10:58 -0700 |
| parents | python/bind.py@efeb2cc78f30 |
| children |
comparison
equal
deleted
inserted
replaced
| 525:37f8bc525888 | 526:4063bbeaa7d4 |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 """ | |
| 4 illlustrate e.g. method bind for python | |
| 5 """ | |
| 6 | |
| 7 class Foo(object): | |
| 8 | |
| 9 @classmethod | |
| 10 def create(cls): | |
| 11 """create an instance and bind a method onto foo""" | |
| 12 class decorator(object): | |
| 13 def __init__(self, function): | |
| 14 self.function = function | |
| 15 def __call__(self): | |
| 16 print "Bar!" | |
| 17 return self.function() | |
| 18 | |
| 19 instance = cls() | |
| 20 instance.foo = decorator(instance.foo) | |
| 21 return instance | |
| 22 | |
| 23 def foo(self): | |
| 24 print "Foo!" | |
| 25 | |
| 26 if __name__ == '__main__': | |
| 27 foo = Foo.create() | |
| 28 foo.foo() |
