Earlier quoted context omitted.
In javascript you can modify an object var foo = Object(); foo.blah = function(x,y) { ... }; But in python, that doesn't quite work. You can only do foo = object() foo.blah = lambda x,y: ... lambdas are a bit more limited as they are restricted to one line, and you can't have print statements, which makes complicated expressions rather ugly. edit: ah, as someone noted, the second code snippet should be something like…
That's not quite right. While you're correct that lambda statements are restricted, I have never actually seen a lambda expression used to extend an object. Instead, you use a named function, which has none of these restrictions: foo = object() foo.name = "Hi thar" def hello(self): print "Hello, I'm %s" % self.name foo.hello = hello This is, in fact, one of the reasons why the self parameter is explicit.
In [21]: def hello(self):
....: print self.name
....:
In [22]: class Foo(object):
....: name = 'blah'
....:
In [23]: goo = Foo()
go
In [24]: goo.hi = hello
In [25]: goo.hi
Out[25]:
In [26]: goo.hi()
TypeError Traceback (most recent call last)
TypeError: hello() takes exactly 1 argument (0 given)