Hacker News new | ask | show | jobs
by reinhardt 5298 days ago
The above doesn't really work as the builtin object class doesn't allow extra attributes. Still any pure Python class does by default:

    class Foo(object):
        pass
    foo = Foo()
    foo.name = ...
    def hello(self): 
        ...
    foo.hello = hello
1 comments

You should either remove `self` or bind it differently:

  foo = Foo()
  def hello():
      print("hello, %s" % (foo,))
  foo.hello = hello
`foo` is an instance therefore `self == foo` already.

  def hello(self):
      print("hello, %s" % (self,))
  foo.hello = types.MethodType(hello, foo)