1

I have this method that takes in an unknown number of args and I have some variables from another class that I want to match the arg variable name to. The variable names are the same across this current class and the OtherClass. I was thinking something like:

def my_method(self, *args):
    for arg in args:
        OtherClass.arg_variable_name = arg # where "arg" on the right is the value

How could I do this effectively in python?

2
  • You probably want to use a dict Commented Feb 11, 2015 at 18:24
  • x=5;def n(p):print "varname is ?"; n(x) ; is the var name of 5 x or p ? multiple names can point to the same object... what is the actual variable name of the object? Commented Feb 11, 2015 at 18:34

2 Answers 2

3

I think you should use **kwds instead of args. *args are values, so the caller can send it not with the variable, but just value itself.

my_object.my_method(1, 2, 3, "string")

If you use **kwds, you'll know the key, value pairs.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use something like

dict( (name,eval(name)) for name in['self','args']

To get a dict with the values for each argument, and then from there you could match the dict values with your respective arg variable name.

Comments

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.