* hacks.py: Added make_unbound_method to allow general function to participate
as hacked class methods.
This commit is contained in:
18
hacks.py
18
hacks.py
@@ -1,5 +1,5 @@
|
|||||||
#
|
#
|
||||||
# $Id: hacks.py,v 1.1 2011-06-03 21:31:59 wirawan Exp $
|
# $Id: hacks.py,v 1.2 2011-06-08 15:49:04 wirawan Exp $
|
||||||
#
|
#
|
||||||
# Created: 20110603
|
# Created: 20110603
|
||||||
# Wirawan Purwanto
|
# Wirawan Purwanto
|
||||||
@@ -11,7 +11,21 @@
|
|||||||
import sys
|
import sys
|
||||||
import new
|
import new
|
||||||
|
|
||||||
def make_unbound_instance_method(method):
|
def make_unbound_method(method):
|
||||||
"""Generates an unbound instance method from a possibly bound
|
"""Generates an unbound instance method from a possibly bound
|
||||||
instance method."""
|
instance method."""
|
||||||
return new.instancemethod(method.im_func, None, method.im_class)
|
return new.instancemethod(method.im_func, None, method.im_class)
|
||||||
|
|
||||||
|
def make_unbound_method(method):
|
||||||
|
"""Generates an unbound instance method from a possibly bound
|
||||||
|
instance method, or even user-defined function.
|
||||||
|
This is useful for injecting external, completely unrelated function
|
||||||
|
as an instance procedure into a class, without having to subclass
|
||||||
|
the whole thing.
|
||||||
|
CAVEAT: This is a haram trick. Know what you are doing."""
|
||||||
|
try:
|
||||||
|
return new.instancemethod(method.im_func, None, method.im_class)
|
||||||
|
except AttributeError:
|
||||||
|
# Assume this is a static method or user-defined external method
|
||||||
|
# injected into this class.
|
||||||
|
return method
|
||||||
|
|||||||
Reference in New Issue
Block a user