Skip to content Skip to sidebar Skip to footer

Translate String Into Function Argument

While applying some external module method to a class I need to be able to pass different pairs of arg = 'value' to the function, like: Ad.nodes.get(id_ = '11974312') How to pass

Solution 1:

To pass multiple arguments to a function, you use the * operator and ** operator as shown below.

def myfoo(*arg, **karg):
    printarg, karg

The * operator pack all ordered argument in it, and the ** operator packs all unmatched key argument in it.

For example:

defmyfoo(a, *arg):
    return a, arg

myfoo(11,22,33)
>> (11, [22, 33])
myfoo(11)
>> (11, [])

For key argument it works the same way,

defmyfoo(a=11, b=22, **kargs):
    return a, b, kargs

myfoo(22, c=100, b=233)
>> (22, 233, {'c': 100})

(ref.)

Solution 2:

You can unfold the positional argument of a function with a single asterisk * and unfold dictionaries as key/value pairs with two asterisks **. For example

defget(a, b, c=0):
    print(a, b, c)

args = (1, 2)
kwargs = {'c': 3}

get(*args, **kwargs)

The Python Reference has details on this.

A more specific example for OP

If you have a function get(id_=None) with the keyword argumentid_ you can use **some_dict to unfold the key/value pairs into keyword arguments. For example

In [1]: defget(id_=None): 
   ...:     print(id_) 
   ...:     # do something with id_ ... 
   ...:                                                                                                                                                                                                   

In [2]: get(**{'id_': 1})                                                                                                                                                                                 
1

In [3]: get(**{'id_': 2})                                                                                                                                                                                 
2

If instead you have a function get(id_) with the positional argumentid_ you can use *some_iterable to unfold the values into positional arguments. You can also use **some_dict to unfold the key/value pairs as long as the keys exactly match the positional arguments. For example

In [4]: def get(id_): 
   ...:     print(id_) 
   ...:     # do something with id_ ... 
   ...:                                                                                                                                                                                                   

In [5]: get(*(1,))                                                                                                                                                                                        
1

In [6]: get(*(2,))                                                                                                                                                                                        
2

In [7]: get(**{'id_': 3})                                                                                                                                                                                 
3

In [8]: # this will fail because `id` is not the argument, `id_` is
In [9]: get(**{'id': 4})                                                                                                                                                                                  
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-9-922e10531f8a> in <module>
----> 1get(**{'id': 4})

TypeError: get() got an unexpected keyword argument 'id'

Post a Comment for "Translate String Into Function Argument"