This file contains 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
def attach_dyn_prop(instance, prop_name, prop_fn): | |
"""Attach prop_fn to instance with name prop_name. | |
Assumes that prop_fn takes self as an argument. | |
Reference: https://stackoverflow.com/a/1355444/509706 | |
""" | |
class_name = instance.__class__.__name__ + 'Child' | |
child_class = type(class_name, (instance.__class__,), {prop_name: property(prop_fn)}) |