Python Forum
How do you use *args and **kwargs?
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you use *args and **kwargs?
#1
I need to know how to use args and kwargs for overloading purposes. Also, if someone knows about generators, explain those to me too.
Reply
#2
There are loads of resource across the internet. Did you have a question?
Reply
#3
def show_me_the_args(*args, **kwargs):
	print(args)
	print('----')
	print(kwargs)
	
show_me_the_args('spam', 42, fish='cooked', dinner=False)

# ('spam', 42)
# ----
# {'dinner': False, 'fish': 'cooked'}
Does that make sense?
Reply
#4
*args and **kwargs are shorthand to put positional arguments in a list, and keyword arguments in a dictionary. They work both ways, you can use them in a call (to use a list or a dict instead of separate args)) or in a function definition (so that the function body can iterate the args, or count them)..

A generator is a function that doesn't "return" but that "yield" instead. "Yield" is like return but a subsequent call to the iterator will start on the instruction that follows the yield. Very well explained here.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question How to compare two parameters in a function that has *args? Milan 4 1,259 Mar-26-2023, 07:43 PM
Last Post: Milan
  How do I instantiate a class with **kwargs? palladium 6 7,305 Feb-16-2023, 06:10 PM
Last Post: deanhystad
  kwargs question Jeff_t 8 2,953 Feb-16-2022, 04:33 PM
Last Post: Jeff_t
  *args implementation and clarification about tuple status amjass12 10 3,997 Jul-07-2021, 10:29 AM
Last Post: amjass12
  [SOLVED] Good way to handle input args? Winfried 2 2,055 May-18-2021, 07:33 PM
Last Post: Winfried
  Two Questions, *args and //= beginner721 8 3,482 Feb-01-2021, 09:11 AM
Last Post: buran
  Misunderstanding kwargs? Donovan 2 2,262 Aug-04-2020, 08:09 PM
Last Post: Donovan
  **kwargs question DPaul 10 4,308 Apr-01-2020, 07:52 AM
Last Post: buran
  does yield support variable args? Skaperen 0 1,669 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  is there a way: repeat key word args Skaperen 2 2,229 Feb-03-2020, 06:03 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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