Thomas Francois
Thomas Francois

Reputation: 866

Pass parameters as unpacked array

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.

Upvotes: 3

Views: 165

Answers (1)

Olivier Melançon
Olivier Melançon

Reputation: 22294

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')

Upvotes: 2

Related Questions