Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 206 characters in body
Source Link
GWW
  • 44.3k
  • 11
  • 119
  • 110

For Python 2.X you can use:

import types
class C:
    pass

def f(self):
    print self

a = C()
a.f = types.MethodType(f,a)
a.f()

For Python 3.X:

import types
class C(object):
    pass

def f(self):
    print(self)

a = C()
a.f = types.MethodType(f,a)
a.f()
import types
class C:
    pass

def f(self):
    print self

a = C()
a.f = types.MethodType(f,a)
a.f()

For Python 2.X you can use:

import types
class C:
    pass

def f(self):
    print self

a = C()
a.f = types.MethodType(f,a)
a.f()

For Python 3.X:

import types
class C(object):
    pass

def f(self):
    print(self)

a = C()
a.f = types.MethodType(f,a)
a.f()
Source Link
GWW
  • 44.3k
  • 11
  • 119
  • 110

import types
class C:
    pass

def f(self):
    print self

a = C()
a.f = types.MethodType(f,a)
a.f()