Python Forum
pass value to function with * and/or **
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pass value to function with * and/or **
#3
I think there is misunderstanding.
boo will be a tuple, hoo will be a dict.
if you call the function with [something like] list or dict you either unpack it during the call (in effect passing individual elements) or you will end up with 2 arguments in boo tuple and empty dict:
def foo(*boo, **hoo):
    print(type(boo), boo)
    print(type(hoo), hoo)

spam = [1, 2]
eggs = {'a':3, 'b':4}

foo(*spam, **eggs)
foo(spam, eggs)
foo(5, 6, c=7, d=8)
Output:
<class 'tuple'> (1, 2) <class 'dict'> {'a': 3, 'b': 4} <class 'tuple'> ([1, 2], {'a': 3, 'b': 4}) <class 'dict'> {} <class 'tuple'> (5, 6) <class 'dict'> {'c': 7, 'd': 8}
the idea is to allow having variable number of positional (*args) or keyword (**kwargs) arguments. *args and **kwargs is conventional way to name these.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: pass value to function with * and/or ** - by buran - Jan-10-2020, 08:12 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020