I've been trying to create a class that gets the parameter names and subsequent values from the init method of another class. Here's an example of the class I'm trying to get those names from:
class Item(Watcher):
def __init__(self, iname: str, iid=0):
super().__init__()
self.iname = iname
self.iid = iid
def __repr__(self):
return f"{self.iname}"
What I would like for the parent class to do is take the function parameters (in this case, "iname" and "iid", but should work for any function with differing parameter names), as well as the values for those parameters and spit them back out in dictionary form like so:
{"iname": "iname's value", "iid": "iid's value"}
So far, I've tried this (from Getting list of parameter names inside python function):
class Watcher:
def __init__(self):
pass
def root(self):
params = self.__init__.__code__.co_varnames
a = {}
for param in params:
if not param == "self": out[param] = "???"
But this is where I get stuck. I'm not quite sure how to dynamically get the values of init parameters given a string of the parameter's name.
If anyone can help, thanks ahead of time