#!/usr/bin/env python
"""Example on how how to override instance method in Python.
See http://stackoverflow.com/q/4243586
"""
import types
from operator import methodcaller


class Spam:
    def update(self):
        print('updating spam!')


class SpamLite(Spam):
    def update(self):
        Spam.update(self)
        print('this spam is lite!')


def poison(spam):
    tmp = spam.update

    def newUpdate(self):
        tmp()
        print('this spam has been poisoned!')
    spam.update = types.MethodType(newUpdate, spam, spam.__class__)


def poisonSpam():
    old = Spam.update

    def newUpdate(self):
        old(self)
        print('poison whole class')
    Spam.update = types.UnboundMethodType(newUpdate, None, Spam)


L = [Spam(), SpamLite()]

map(methodcaller('update'), L)

print("*"*79)
poisonSpam()
map(methodcaller('update'), L)

print("*"*79)
map(poison, L)
map(methodcaller('update'), L)

78b07a56509785e800577bbe25d606a3a4c2e33d
<mask token>


class Spam:

    def update(self):
        print('updating spam!')


class SpamLite(Spam):

    def update(self):
        Spam.update(self)
        print('this spam is lite!')


<mask token>


def poisonSpam():
    old = Spam.update

    def newUpdate(self):
        old(self)
        print('poison whole class')
    Spam.update = types.UnboundMethodType(newUpdate, None, Spam)


<mask token>

<mask token>


class Spam:

    def update(self):
        print('updating spam!')


class SpamLite(Spam):

    def update(self):
        Spam.update(self)
        print('this spam is lite!')


def poison(spam):
    tmp = spam.update

    def newUpdate(self):
        tmp()
        print('this spam has been poisoned!')
    spam.update = types.MethodType(newUpdate, spam, spam.__class__)


def poisonSpam():
    old = Spam.update

    def newUpdate(self):
        old(self)
        print('poison whole class')
    Spam.update = types.UnboundMethodType(newUpdate, None, Spam)


<mask token>

<mask token>


class Spam:

    def update(self):
        print('updating spam!')


class SpamLite(Spam):

    def update(self):
        Spam.update(self)
        print('this spam is lite!')


def poison(spam):
    tmp = spam.update

    def newUpdate(self):
        tmp()
        print('this spam has been poisoned!')
    spam.update = types.MethodType(newUpdate, spam, spam.__class__)


def poisonSpam():
    old = Spam.update

    def newUpdate(self):
        old(self)
        print('poison whole class')
    Spam.update = types.UnboundMethodType(newUpdate, None, Spam)


<mask token>
map(methodcaller('update'), L)
print('*' * 79)
poisonSpam()
map(methodcaller('update'), L)
print('*' * 79)
map(poison, L)
map(methodcaller('update'), L)

<mask token>


class Spam:

    def update(self):
        print('updating spam!')


class SpamLite(Spam):

    def update(self):
        Spam.update(self)
        print('this spam is lite!')


def poison(spam):
    tmp = spam.update

    def newUpdate(self):
        tmp()
        print('this spam has been poisoned!')
    spam.update = types.MethodType(newUpdate, spam, spam.__class__)


def poisonSpam():
    old = Spam.update

    def newUpdate(self):
        old(self)
        print('poison whole class')
    Spam.update = types.UnboundMethodType(newUpdate, None, Spam)


L = [Spam(), SpamLite()]
map(methodcaller('update'), L)
print('*' * 79)
poisonSpam()
map(methodcaller('update'), L)
print('*' * 79)
map(poison, L)
map(methodcaller('update'), L)

#!/usr/bin/env python
"""Example on how how to override instance method in Python.
See http://stackoverflow.com/q/4243586
"""
import types
from operator import methodcaller


class Spam:
    def update(self):
        print('updating spam!')


class SpamLite(Spam):
    def update(self):
        Spam.update(self)
        print('this spam is lite!')


def poison(spam):
    tmp = spam.update

    def newUpdate(self):
        tmp()
        print('this spam has been poisoned!')
    spam.update = types.MethodType(newUpdate, spam, spam.__class__)


def poisonSpam():
    old = Spam.update

    def newUpdate(self):
        old(self)
        print('poison whole class')
    Spam.update = types.UnboundMethodType(newUpdate, None, Spam)


L = [Spam(), SpamLite()]

map(methodcaller('update'), L)

print("*"*79)
poisonSpam()
map(methodcaller('update'), L)

print("*"*79)
map(poison, L)
map(methodcaller('update'), L)
