Given an object already created, what is the best way to figure out how it was created? Basically, I would like to figure out the actual parameters during the object creation (eventually the actual parameters passed to the class init method).
Note that I mean the actual parameters here, not the formal parameters. The formal parameters may perhaps accept 20 different parameters, maybe 16 of them are optional with default values each. In the actual call (i.e. creating the object instance), perhaps only 5 parameters are given, and 3 of them happens to be the same as default value.
What I would like to do is to retrieve these 5 parameters and their values, after the object has been created.
For code example, lets say the class of interest is called Kmodel. Kmodel constructor accepts 20 different parameters (16 of them don't need to be specified by caller).
In caller's code, this would be something like:
kmodel = Kmodel( param1 = 10, param2 = None, param3 = "name3", param4 = [1, 3, 5], param5 = dict("keyx": "valuey"}) ...
Later call a method to find out the actual parameters calling Kmodel( )
figure_out_actual_params(kmodel)Now the implementation of the code to figure out:
def figure_out_acutal_params( kmodel ): ...
here is the code that needs to be implemented to reconstruct the actual parameters calling Kmodel creation
- I have examined the inspect module. While inspect can dump out a lot of info, it is not clear to me how to find out the actual param was supplied during the actual creation of the object kmodel.
Thanks!
PS. Note that there are several similar questions on this topic and I have read them all. I don;t think this question is the same. For my question, I can query all the internal object attributes of an object, and I still won't be able to tell which one has been specified during the actual object construction. (The sticky issue is the actual parameter may happen to be the default value.)