Python object type listing after module reload? [to change the code on the fly]

I am launching an interactive python session that creates large python data structures (5+ GB) that take a lot of time to load, and therefore I want to use Python on-the-the ( ), maximum (although sometimes without having to plan too much for of this ).

My current problem is this: I have an old instance of the class, which I later modified the code and reloaded the module. I would like the old instance to be able to use the new function definitions. How can I do this without manually copying all the information from the old instance to the new fresh instance?

Here is what I have tried. Suppose I have an M.py module:

 class A(): def f(self): print "old class" 

Here is an interactive session:

 import M old_a = Ma() # [suppose now I change the definition of MAf in the source file] reload(M) # I attempt to use the new class definition with the old instance: MAf(old_a) 

at this moment I get the following type error from Python:

 TypeError: unbound method f() must be called with A instance as first argument (got A instance instead) 

Python is obviously not happy to get the old instance of A, although they are basically functionally equivalent (in my code) - is there a way that I could β€œtype” it for a new instance type so that Python can’t complain? Something moral: MAf( (MA) old_a ) ?

+4
source share
2 answers

There is no casting in Python, but you can change the class of an existing object: it is completely legal and does the job:

 old_a.__class__=MA old_a.f() 

Until you change the relationship between class methods and instance variables, change what __init__ does or something like that, that's fine.

EDIT: As jsbueno points jsbueno : __init__ or __new__ do not call __new__ at the change __class__ . In addition, a new __del__ will be called upon destruction.

+5
source

Since you cannot roll, you need to review your code so that these cryptic changes on the fly can work.

Step 1. Separate the algorithm from the data. Write a very simple (and very unlikely to change) class for raw Data. Often a list of named tuples is all you ever need.

Step 2. Create algorithms that work on data objects by wrapping them instead of updating.

Like this.

 def some_complex_algo( list_of_named_tuples ): for item in list_of_named_tuples: # some calculation yield NewTuple( result1, result2, ..., item ) 

Now you can try your processing:

 result = list( some_complex_algo( source_data ) ) 

If you do not like the result, you only need to override some_complex_algo and re-run it. source_data untouched. Indeed, this may be unchanged.

0
source

Source: https://habr.com/ru/post/1395077/


All Articles