i'd make copy of class, while updating of methods refer new set of __globals__
i thinking below, unlike types.functiontype
, constructor types.unboundmethodtype
not accept __globals__
, suggestions how work around this?
def copy_class(old_class, new_module): """copies class, updating __globals__ of methods point new_module""" new_dict = {} name, entry in old_class.__dict__.items(): if isinstance(entry, types.unboundmethodtype): entry = types.unboundmethodtype(name, none, old_class.__class__, globals=new_module.__dict__) new_dict[name] = entry return type(old_class.name, old_class.__bases__, new_dict)
the __dict__
values functions, not unbound methods. unbound method objects created on attribute access. if are seeing unbound method objects in __dict__
, weird happened class object before function got it.