python - List function parameters not equal to their default values -



python - List function parameters not equal to their default values -

given python function definition of form:

def foo(a=none, b=1, c='bar'):

how can dynamically determine parameters have been set value different default value? instance:

foo(48)

i want able dynamically identify a parameter set. solution i'm seeking go on work if added additional parameters signature (i.e. don't want manual check if == none, etc.).

==update==

to clarify goal: users of function foo, allow executed regardless. if identify user of user type/class/category bar want allow succeed only if called foo argument parameter a. if provided arguments (or @ least, arguments not equal defaults) other parameter exception should raised. (i know user calling function based on other global data).

again, if b != 1 or c != 'bar', have update every time foo's signature gets modified.

i think best course of study of action utilize inspect.

import inspect def foo(non_default_var, a=none, b=1, c='bar'): inspector = inspect.getargspec(foo) local_vars = inspector.args # = ['d', 'a', 'b', 'c'] default_values = inspector.defaults # = (none, 1, 'bar') default_vars = local_vars[-len(default_values):] # since default values @ end var, default_value in zip(default_vars, list(default_values)): # iterate , compare print locals()[var] == default_value # true if variable default

i guess work improve decorator.

the drawback cannot know if user intended utilize default value, or entered value explicitely in function call.

python python-2.7

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -