Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay, here's a proof-of-concept that shows how to do it. Note that it only goes one level deep -- properties and nested functions are not adjusted. To implement that, as well as make this more robust, each function's globals() should be compared to the globals() that should be replaced, and only make the substitution if they are the same.</p> <pre><code>def migrate_class(cls, globals): """Recreates a class substituting the passed-in globals for the globals already in the existing class. This proof-of-concept version only goes one-level deep (i.e. properties and other nested functions are not changed).""" name = cls.__name__ bases = cls.__bases__ new_dict = dict() if hasattr(cls, '__slots__'): new_dict['__slots__'] = cls.__slots__ for name in cls.__slots__: if hasattr(cls, name): attr = getattr(cls, name) if callable(attr): closure = attr.__closure__ defaults = attr.__defaults__ func_code = attr.__code__ attr = FunctionType(func_code, globals) new_dict[name] = attr if hasattr(cls, '__dict__'): od = getattr(cls, '__dict__') for name, attr in od.items(): if callable(attr): closure = attr.__closure__ defaults = attr.__defaults__ kwdefaults = attr.__kwdefaults__ func_code = attr.__code__ attr = FunctionType(func_code, globals, name, defaults, closure) if kwdefaults: attr.__kwdefaults__ = kwdefaults new_dict[name] = attr return type(name, bases, new_dict) </code></pre> <p>After having gone through this excercise, I am <em>really</em> curious as to why you need to do this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload