Is there a cleaner way to get the defaults of a method than using the inspect module? I’d like to do this:
class C(object):
def __init__(self,Type='generic',X=5,Y=500):
defaults=getDefaults() #not sure about this line
typeDefaults_X={'short':'1','long':'10'}
typeDefaults_Y={'large':2000,'small':100}
if X == defaults['X']:
X = typeDefaults_X.get(Type, defaults['X'])
if Y == defaults['Y']:
Y = typeDefaults_Y.get(Type, defaults['Y'])
I know that I could do this by:
defaults=dict(zip(inspect.getargspec(C.__init__).args[1:],inspect.getargspec(C.__init__).defaults))
but it seems so terribly un-pythonic.
It’s not really clear to me what you are trying to do. But in the method, variable already has the specified default value. But it seems to me that you would be better off not using formal default arguments in this case.
Is clearer I think, but even this can be a little hard to follow. You might rethink your interface.