Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass parameters as unpacked array

Tags:

python

I came across a blog post with the following function call:

intersection_cardinality = len(set.intersection(*[x, y]))

Is there any benefit in passing parameters as an unpacked array created just for that, instead of simply calling set.intersection(x, y)?

The blog post was written in python2, but the question goes for 3 as well.

like image 862
Thomas Francois Avatar asked Dec 08 '25 21:12

Thomas Francois


1 Answers

In the example you provided, there is no real point in using this syntax.

There are cases though where creating an array or tuple to unpack it can be useful.

# Python3

def print_n_times(n, string):
    # Instead of doing a loop we unpack an array of length n
    print(*n*(string,), sep='\n')
like image 56
Olivier Melançon Avatar answered Dec 11 '25 12:12

Olivier Melançon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!