-1

I gather from answers like this that Python's built-in type set has a number of useful class methods, such as set.intersection(). Where are they documented? I haven't found them documented here, which seems to cover only the instance methods.

Here's an example demonstrating the existence of one of these class methods:

>>> set.intersection(set([1, 2, 3]), set([2, 3, 4]))
{2, 3}

>>> set.intersection([1, 2, 3], [2, 3, 4])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>

>>> set([1, 2, 3, 4]).intersection([1, 2, 3], [2, 3, 4])  # same args, passed to instance method
{2, 3}

The documentation for the instance method says that it accepts any iterable as an argument, not just a set. The example above shows that the class method works differently.

It would be nice to know what arguments you are supposed to be able to pass to these functions without having to guess and test.

martineau
  • 119,623
  • 25
  • 170
  • 301
Ben Kovitz
  • 4,920
  • 1
  • 22
  • 50
  • Right, because as juanpa.arrivillaga says, it’s an instance method. The error you edited in gives evidence for that (compare `set.intersection({1, 2, 3}, [2, 3, 4])` with `set.intersection([2, 3, 4], {1, 2, 3})`). – Ry- Dec 09 '19 at 02:20
  • What? Again, that is not a classmethod. That is an instance method, you are just manually passing the instance as the first argument. So, consider, `x = []; list.append(x, 1); print(x)`. In Python `my_instance.method()` is *mostly* equivalent to `MyClass.method(instance)`. – juanpa.arrivillaga Dec 09 '19 at 02:21
  • Note, if it were a classmethod, you could do `isintance(vars(set)['intersection'], classmethod)` and it would evaluate to `True`. But it doesn't. Again, this is not a class method, and you've provided evidence of this fact in the question itself. – juanpa.arrivillaga Dec 09 '19 at 02:26
  • To those who are downvoting this question: Please consider that a question based on a confused or mistaken premise can be useful, since it provides an opportunity to write an answer that corrects the confusion or mistake. – Ben Kovitz Dec 09 '19 at 03:16

1 Answers1

1

set.intersection() is an instance method. The instance is passed as the first argument implicitly.

Alec
  • 8,529
  • 8
  • 37
  • 63