diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/example/bind.py	Tue Sep 24 13:10:58 2013 -0700
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+
+"""
+illlustrate e.g. method bind for python
+"""
+
+class Foo(object):
+
+    @classmethod
+    def create(cls):
+        """create an instance and bind a method onto foo"""
+        class decorator(object):
+            def __init__(self, function):
+                self.function = function
+            def __call__(self):
+                print "Bar!"
+                return self.function()
+
+        instance = cls()
+        instance.foo = decorator(instance.foo)
+        return instance
+
+    def foo(self):
+        print "Foo!"
+
+if __name__ == '__main__':
+    foo = Foo.create()
+    foo.foo()