Mercurial > hg > config
annotate python/example/bind.py @ 924:fc88a12acf48 default tip
add lfs filter
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 04 Dec 2024 15:50:53 -0800 |
parents | 4063bbeaa7d4 |
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() |