I think this question boils down to the difference between named and positional arguments.
Let's define a function foo(bar)
:
def foo(bar):
print(bar, "(type: " + str(type(bar)) + ")")
Now, let's run some examples:
foo(1)
# 1 (type: <class 'int'>)
This is a positional argument, it goes into the function in the order you give it.
In this case, there is only one argument, so it goes into the first position of the foo
function, which is the argument bar
.
foo(bar=1)
# 1 (type: <class 'int'>)
This is the named equivalent of the previous version. Instead of feeding it into the function as the n-th argument, it explicitely feeds it into the argument named bar
.
foo('bar=1')
# bar=1 (type: <class 'str'>)
This is a positional argument, with the string 'bar=1'
. Python doesn't care what the content of that string is, it is and stays the string with the content 'bar=1'
.
I suspect what you are trying to do is to feed the function named arguments, but with the arguments defined in a dict-like structure. You can achieve that like this:
args = {'bar': 1}
foo(**args)
# 1 (type: <class 'int'>)
This way, the dict args
gets taken as named arguments of that function.
Now back to the question you originally had, which is, how to use the string 'bar=1'
to achieve the same result. I don't think this is easily possible. It would involve parsing the string manually, then setting up that dict of named arguments. I'm not sure what you are trying to do with it, but there must surely be a better solution than using strings for that.