By Dmitriy Grankin


2019-02-11 09:50:57 8 Comments

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.

2 comments

@Andrew F 2019-02-11 09:57:18

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'

@Dmitriy Grankin 2019-02-11 10:24:23

I was misunderstood for how I defined my question initially, please consider my edited question again. Thank you.

@Mayur 2019-02-11 09:55:06

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.)

@Dmitriy Grankin 2019-02-11 10:24:11

I was misunderstood for how I defined my question initially, please consider my edited question again. Thank you.

Related Questions

Sponsored Content

12 Answered Questions

[SOLVED] Is there a way to substring a string?

  • 2009-03-19 17:29:41
  • Joan Venge
  • 2355928 View
  • 1764 Score
  • 12 Answer
  • Tags:   python string

6 Answered Questions

[SOLVED] How to lowercase a string in Python?

18 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

10 Answered Questions

[SOLVED] Calling a function of a module by using its name (a string)

  • 2008-08-06 03:36:08
  • ricree
  • 540669 View
  • 1355 Score
  • 10 Answer
  • Tags:   python object

16 Answered Questions

[SOLVED] Convert bytes to a string?

16 Answered Questions

[SOLVED] How to make a chain of function decorators?

32 Answered Questions

[SOLVED] "Least Astonishment" and the Mutable Default Argument

20 Answered Questions

[SOLVED] Converting string into datetime

  • 2009-01-21 18:00:29
  • Oli
  • 2216643 View
  • 1733 Score
  • 20 Answer
  • Tags:   python datetime

14 Answered Questions

[SOLVED] How to return multiple values from a function?

18 Answered Questions

[SOLVED] Using global variables in a function

Sponsored Content