Dmitriy Grankin
Dmitriy Grankin

Reputation: 618

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 dicts or tuples to the function, so that it recognises 'id_' (string) as id_ (argument) in ('id_', '11974312') (tuple) or {'id_':'11974312'} (dictionary) ?

Basically, I just need to get id_ out of 'id_'

For your reference, I am trying to use neomodel module for neo4j graph db.

Upvotes: 1

Views: 119

Answers (2)

Andrew F
Andrew F

Reputation: 2950

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

def get(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 argument id_ you can use **some_dict to unfold the key/value pairs into keyword arguments. For example

In [1]: def get(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 argument id_ 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>
----> 1 get(**{'id': 4})

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

Upvotes: 1

Mayur
Mayur

Reputation: 2731

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

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

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

For example:

def myfoo(a, *arg):
    return a, arg

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

For key argument it works the same way,

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

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

(ref.)

Upvotes: 2

Related Questions