annotate python/example/bind.py @ 540:8c9cd7472d28

.bashrc
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 27 Sep 2013 11:50:54 -0700
parents 4063bbeaa7d4
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
efeb2cc78f30 stub this motha
Jeff Hammel <jhammel@mozilla.com>
parents: 263
diff changeset
12 class decorator(object):
efeb2cc78f30 stub this motha
Jeff Hammel <jhammel@mozilla.com>
parents: 263
diff changeset
13 def __init__(self, function):
efeb2cc78f30 stub this motha
Jeff Hammel <jhammel@mozilla.com>
parents: 263
diff changeset
14 self.function = function
efeb2cc78f30 stub this motha
Jeff Hammel <jhammel@mozilla.com>
parents: 263
diff changeset
15 def __call__(self):
efeb2cc78f30 stub this motha
Jeff Hammel <jhammel@mozilla.com>
parents: 263
diff changeset
16 print "Bar!"
efeb2cc78f30 stub this motha
Jeff Hammel <jhammel@mozilla.com>
parents: 263
diff changeset
17 return self.function()
efeb2cc78f30 stub this motha
Jeff Hammel <jhammel@mozilla.com>
parents: 263
diff changeset
18
efeb2cc78f30 stub this motha
Jeff Hammel <jhammel@mozilla.com>
parents: 263
diff changeset
19 instance = cls()
efeb2cc78f30 stub this motha
Jeff Hammel <jhammel@mozilla.com>
parents: 263
diff changeset
20 instance.foo = decorator(instance.foo)
efeb2cc78f30 stub this motha
Jeff Hammel <jhammel@mozilla.com>
parents: 263
diff changeset
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
efeb2cc78f30 stub this motha
Jeff Hammel <jhammel@mozilla.com>
parents: 263
diff changeset
26 if __name__ == '__main__':
efeb2cc78f30 stub this motha
Jeff Hammel <jhammel@mozilla.com>
parents: 263
diff changeset
27 foo = Foo.create()
efeb2cc78f30 stub this motha
Jeff Hammel <jhammel@mozilla.com>
parents: 263
diff changeset
28 foo.foo()