Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
**kwargs question
#8
(Mar-29-2020, 08:02 AM)buran Wrote: def some_func(*args, **kwargs):
I liked the example, it was very clear. But as DPaul said, why those asterisks? It doesn't make very clear what is going on. I think he would like to see something like:
def some_func(args=tuple(), kwargs=dict()):
This makes clear what the arguments should be. And we prefer clear programs, don't we?
But then I tried to make it work. It appeared the calls to the function had to be altered:
some_func(('foo',), {'spam':1})   #was: some_func('foo', spam=1)
some_func(('foo', 'bar'),         #was: some_func('foo', 'bar', spam=1, eggs=2)
          {'spam':1, 'eggs':2})
some_func(pos)                    #was: some_func(*pos)
some_func((), keyw)               #was: some_func(**keyw)
some_func(('foo',), keyw)         #was: some_func('foo', **keyw)
some_func(pos, keyw)              #was: some_func(*pos, **keyw)
Then I saw it didn't get much clearer after all. So the asterisks serve a purpose.
Reply


Messages In This Thread
**kwargs question - by DPaul - Mar-29-2020, 07:48 AM
RE: **kwargs question - by buran - Mar-29-2020, 08:02 AM
RE: **kwargs question - by DPaul - Mar-29-2020, 08:43 AM
RE: **kwargs question - by ndc85430 - Mar-29-2020, 08:45 AM
RE: **kwargs question - by buran - Mar-29-2020, 08:55 AM
RE: **kwargs question - by DPaul - Mar-29-2020, 09:34 AM
RE: **kwargs question - by buran - Mar-29-2020, 09:41 AM
RE: **kwargs question - by DPaul - Apr-01-2020, 07:04 AM
RE: **kwargs question - by ibreeden - Mar-29-2020, 12:11 PM
RE: **kwargs question - by buran - Mar-29-2020, 02:11 PM
RE: **kwargs question - by buran - Apr-01-2020, 07:52 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I instantiate a class with **kwargs? palladium 6 8,004 Feb-16-2023, 06:10 PM
Last Post: deanhystad
  kwargs question Jeff_t 8 3,217 Feb-16-2022, 04:33 PM
Last Post: Jeff_t
  Misunderstanding kwargs? Donovan 2 2,367 Aug-04-2020, 08:09 PM
Last Post: Donovan
  Unpacking dictionary from .xlsx as Kwargs etjkai 5 3,020 Dec-27-2019, 05:31 PM
Last Post: etjkai
  opts vs kwargs Skaperen 4 2,605 Nov-30-2019, 04:57 AM
Last Post: Skaperen
  create dictionary from **kwargs that include tuple bluefrog 2 5,023 Oct-26-2016, 10:24 PM
Last Post: Larz60+
  How do you use *args and **kwargs? Gengar 3 20,274 Sep-20-2016, 04:22 PM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

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