List passing and dictionary type parameter using Python

When i run this code

def func(x, y, *w, **z):
  print x
  print y
  if w:
      print w

  if z:
      print z
  else:
      print "None"

func(10,20, 1,2,3,{'k':'a'})

I get the result as follows.

10
twenty
(1, 2, 3, {'k': 'a'})
None

But I expected the following: I mean the list parameters (1,2,3) corresponding to * w and the dictionary correspondence ** z.

10
twenty
(1,2,3)
{'k': 'a'}

Q: What went wrong? How to pass a list and dictionary as parameters?

Added

func(10,20, 10,20,30, k='a')

seems to work

+3
source share
3 answers

Place two stars in front of the dictionary:

func(10,20, 1,2,3,**{'k':'a'})
+7
source

I'm not sure what the input format is, but this will work:

func(10,20, 1,2,3, k='a')

k = a , . 1,2,3 "" ( ?) arg.

+2

If you want to be more explicit, you can do

func(10,20,*[1,2,3],**{'k':'a'})

indicate (to the reader) which argument you want to use with each parameter of the special form.

+1
source

Source: https://habr.com/ru/post/1742870/


All Articles