-1

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.

mrSaraf
  • 123
  • 1
  • 13
Dmitriy Grankin
  • 568
  • 9
  • 21

2 Answers2

6

If I understand your question correctly, you are looking for the ** operator.

Example:

kwargs = {'first': 3, 'second': 6}

def add(first, second):
    return first + second

print(add(**kwargs) == 9)

This will print True. When you apply ** to a dict argument, it will be decomposed into keyword arguments.

gmds
  • 19,325
  • 4
  • 32
  • 58
0

The argument name can be read as string using inspect.signature(function name).parameters.keys() where function name is a name of function, which argument need to be read as string

Example:

    import inspect, itertools 
dictionary={'id':'David','ip':'11.1.1.20'}
def func(id,ip):
                    func_argument = list(inspect.signature(func).parameters.keys() )
                    print(func_argument)
                    #Print the value from dic for each argument which is key in dict
                    for i in func_argument:
                        print(dictionary[i])                    
func(id=100,ip=200)
Pradeep Pandey
  • 307
  • 2
  • 7