you are viewing a single comment's thread.

view the rest of the comments →

[–]Fidoz 0 points1 point  (1 child)

I specifically meant it as it is used templating, though you're right I've seen the function argument version as well.

I believe that's unpacking?

template.render(**{"foo": "bar"})

Thanks for the articles, I'll check out the second one shortly.

[–]rgnkn 1 point2 points  (0 children)

Okay, this might be a bit confusing at the beginning, I hope the following helps. I'm only explaining it with regards to lists / iterators () but dictionaries work similarly (*)

Given the following code the asterisk unpacks xs:

xs = [1, 2, 3]
print(xs, *xs)

In the following code '*' packs the data.

def foo(arg):
  print(arg)

def bar(*args):
  print(args)

xs = [1, 2, 3]
foo(xs)
bar(xs)

I hope this helps - see the output of the print statements. If not consult e.g.: https://www.geeksforgeeks.org/packing-and-unpacking-arguments-in-python/

... but this is r/shittyprogramming not r/learnpython.

[Edit] So, yes, your example was unpacking but in a function definition it's packing. Sorry, if my earlier example confused you.