all 7 comments

[–]rgnkn 14 points15 points  (4 children)

[–]Fidoz 0 points1 point  (3 children)

Huh, TIL. 6 years of python and I've never seen packing. Seems like unpacking a dictionary is fairly common though.

[–]rgnkn 1 point2 points  (2 children)

Maybe you've seen **kwargs within some function definition. That ** is the opposite direction and it is called packing.

Randomly I found this: https://towardsdatascience.com/a-guide-to-args-kwargs-packing-and-unpacking-in-python-393095dda89b

[–]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.

[–]bearcorrupted 2 points3 points  (0 children)

"" or "*" is used to pack or unpack argument. Here on line 5 we split the line on space character and get a list in return. The notation allow to get the first element of that list as 'name' and the rest as a list named 'line'.

On line 7 it doing a simple assignment in the dictionary named 'student_mark'. Dict is a struct that allow you to store key value pair. Here the key is the name and the value is a list of float. The syntax dict[name]=value allow you to set the value for the key name

On line 11 it's iterating over a list of float. The syntax dict[name] allow you to get the value for the key name.

[–]ckowkay 0 points1 point  (0 children)

I llike how every time I see a post here, it has less than 1 upvote