Created
August 11, 2011 05:01
-
-
Save cdleary/1138936 to your computer and use it in GitHub Desktop.
Rebinding in Python3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import types | |
from collections import namedtuple | |
class Oven: | |
def __init__(self): | |
self.pie = 'apple' | |
def easy_bake(self): | |
return self.pie | |
oven = Oven() | |
bound_method = oven.easy_bake | |
ovenish = namedtuple('OvenLikeThing', ['pie'])('cherry') | |
# Note: if you try to make a MethodType using bound_method, you get a type | |
# error on invocation, because of having too many things bound. | |
new_bound_method = types.MethodType(bound_method.__func__, ovenish) | |
assert new_bound_method() == 'cherry' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment