3 ms·
In a language without closures [1], you'd do something like this: class __AnimationHandler(superclass): def __init__(self, obj): self.o
by yummyfajitas 15y ago
In a language without closures [1], you'd do something like this:
class __AnimationHandler(superclass):
def __init__(self, obj):
self.obj = obj
def animations(self):
self.obj.position.x += 10.0
def onCompletion(self, finished):
self.obj.destroy()
ui_view.animateWithDuration(0.2, handler=__AnimationHandler)
Although I definitely prefer closures, this is hardly a clusterfuck. But it would be fuglier in C/C++.
[1] Yes, python has closures, which obey confusing rules and often work. But the pythonic way is an object handler.
- lucian1900 15y agoPython's closures are exactly like, say, scheme's, except that bindings are local by default.
- yummyfajitas 15y agoNo, they aren't exactly the same: x = 0 def foo(): x = x + 1 print x UnboundLocalError: local variable 'x' referenced before assignment The javascript version works fine. var x = 0; var foo = function() { x = x + 1; console.log(x); } I realize there are ways around this, but they are considered unpythonic. For example: x = [0] def foo(): x[0] = x[0] + 1 print x[0] This works as you'd expect.
- lucian1900 15y agoAs I said, except that bindings are local by default. Python3's nonlocal allows explicit enabling of scheme-like behaviour.