Strategy pattern impossible in pyjamas (can't bind functions to instances) #95

pyjsorg opened this Issue Apr 27, 2012 · 2 comments


None yet

1 participant


Pyjamas doesn't seem to support either types module or get() method for binding a function/method to a class method. This makes the Strategy pattern impossible to implement in Pyjamas.

For example, using types module, a clean way to implement Strategy pattern in Python would be as follows:

import types

def strategyA(possible_self):

instance = OrigObject()
instance.strategy = types.MethodType(strategyA, instance)

This runs fine in Pyjamas Desktop, but throws an import error in the browser:
ImportError: No module named types, types in context None

The alternative way is to use get() method for the function being bound:

instance.strategy = strategyA.get(self, OrigObject)

This too works great in Pyjamas Desktop, but throws a type error in Pyjamas:
TypeError: Object function ... has no method 'get'

Original issue: (July 05, 2011 14:40:28)


From on July 05, 2011 15:40:52:
Here is a modified version of helloworld example that uses Strategy pattern, it's a bit of overkill for this example, but it's simple and it works. This version relies on get() method. Tested it to work in Pyjamas Desktop.


From on July 05, 2011 15:44:42:
Here is an alternative version that uses types.MethodType() instead, it also works in Pyjamas Desktop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment