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

Open
pyjsorg opened this Issue Apr 27, 2012 · 2 comments

Projects

None yet

1 participant

@pyjsorg

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):
pass

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

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: http://code.google.com/p/pyjamas/issues/detail?id=628 (July 05, 2011 14:40:28)

@pyjsorg

From atsep...@gmail.com 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.

@pyjsorg

From atsep...@gmail.com 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