Mercurial > hg > config
annotate python/bind.py @ 498:95ba5770d2f0
file decorator
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 21 Aug 2013 15:54:09 -0700 |
parents | efeb2cc78f30 |
children |
rev | line source |
---|---|
263
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 |
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 """ |
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 illlustrate e.g. method bind for python |
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 """ |
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 |
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 class Foo(object): |
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 |
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 @classmethod |
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 def create(cls): |
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 """create an instance and bind a method onto foo""" |
264 | 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 | |
263
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
22 |
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 def foo(self): |
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 print "Foo!" |
7b15ed0b372c
add a sample to bind to an instance, stub
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
25 |
264 | 26 if __name__ == '__main__': |
27 foo = Foo.create() | |
28 foo.foo() |