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 **
#5
(Jan-10-2020, 08:19 AM)Skaperen Wrote: if the caller does use * and/or **, does what the caller provides get passed through to the function as-is, or does it go through the argument handling mechanisms that give the function a tuple and dictionary
I think I show all possible variations in my example. You can do the same with your own type/class objects and see what the result is. Here it is again

from collections import OrderedDict

def foo(*boo, **hoo):
    print(type(boo), boo)
    print(type(hoo), hoo)
 
spam = [1, 2]
eggs = OrderedDict((('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], OrderedDict([('a', 3), ('b', 4)])) <class 'dict'> {} <class 'tuple'> (5, 6) <class 'dict'> {'c': 7, 'd': 8}
as you can see that when unpacked elements of the list are stored in a tuple and elements of the OrderedDict are stored in a dict
when not unpacked you get list and OrderedDict as elements in a tuple.
bottom line - args/boo will be a tuple always, kwargs/hoo will be a dict always.
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:32 AM

Forum Jump:

User Panel Messages

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