Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRay1 B
9,783 Points.symmetric_difference(other) or .symmetric_difference(*other) in Teachers Notes of Set Math in Python Collections?
Noticed the bottom of Teacher's Notes:
<also: notice how those are using *others? That's a tuple of other sets. See, I told you that pattern was everywhere!>
However,
^ or .symmetric_difference(other)
doesn't include a star. Is there a reason for this?
Thanks!
2 Answers
Steven Parker
231,269 PointsThey call that the "splat" operator.
As used here, it performs argument unpacking. So when you pass other by itself, it's just a single argument (which might be a list). But when you pass it as *other
, then you are taking the list and converting it into individual arguments. For example:
blob = [1, 2, 3]
somefunction(blob) # this passes the list "blob" as a single argument
somefunction(*blob) # this is the same thing as: somefunction(1, 2, 3)
Ray1 B
9,783 PointsOk thanks! I was just confused on why the other set operations in the Teachers Notes had a splat operator but this one didn't.