Search code examples
python-3.xargumentslist-comprehensioninspectiterable-unpacking

How do I unpack this nested object?


Inside a function, I want to print, for all function parameters, "parameter_name=value"

And I want to get the data from inspect.getfullargspec()[0:4:3], which seems to return a tuple of lists with variable names, and variable values.

class someclass(object):
def someFunction(self,
                    var1=10000,
                    varname=[4000,1],
                    somethin=0.1,
                    blabla = 0.4,
                    this="gfjgf"):   
    import inspect
    [print(arg,"=",value,"\n") for j in inspect.getfullargspec(self.someFunction)[0:4:3] for arg,value in zip(j)]  

inspect.getfullargspec(self.someFunction)[0] is a list of strings with the variable names

inspect.getfullargspec(self.someFunction)[3] is a tuple of the variable values

I expect this output:

var1=10000
varname=[4000,1]
somethin=0.1
blabla = 0.4
this=gfjgf

but I get random garbage


Solution

  • This should work if all parameters are keywords:

    for arg,name in zip(self.someFunction.__code__.co_varnames[1:], self.someFunction.__defaults__[1:]):
        print(arg, "=", name)
    

    The [1:] is for slicing away the self parameter.