0

Consider the below code snippet in python

argKey = 'actualArgument'

methodA(argKey=argVal) #invoking methodA

where methodA() is defined as :

def methodA(actualArgument, actualArgument2, actualArgument3)

methodA can be invoked only via named arguments ie. you invoke by specifying the argument name and passing the argument value.

--> throws an error :

TypeError: methodA() got an unexpected keyword argument 'argKey '

I want 'methodA' to be invoked by the value contained in the 'argKey' variable i.e

methodA(actualArgument=argVal)

How to get the substitution of the string into the method invocation call.?

1
  • 2
    It helps to know why you want to do this... Commented May 14, 2012 at 13:54

1 Answer 1

3

To pass parameters with computed names, use a dict:

def foo(a, b):
  return a+b

name_of_a = 'a'
params = {name_of_a: 1, 'b': 2}
print foo(**params) # note the asterisks
Sign up to request clarification or add additional context in comments.

1 Comment

Here are some docs from the tutorial docs.python.org/tutorial/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.