1

I am using macOS, conda python 3.7 with PyCharm CE IDE.

When clicking into the function, the function didn't show any source code. Therefore, where can I find the .difference() code?

enter image description here

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

2 Answers2

2

It's written in C to improve performance (pycharm doesn't have access to the cpython source code, so it can't jump to the definition), you find it here: https://github.com/python/cpython/blob/main/Objects/setobject.c#L1481

The main details of the algorithm are from line 1531 and reasonably easy to follow. It basically iterates the first set, checking if each item is in the other set, if it is, add it to a result set, then returns the result set.

Jonno_FTW
  • 8,601
  • 7
  • 58
  • 90
1

The code you're looking for starts here.

First thing it does is check that the two parameters are the same length. Then it goes through the first and checks for elements that are not present in the second, building up the result as it goes. Finally, it returns said result.

hd1
  • 33,938
  • 5
  • 80
  • 91