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

2
  • 2
    Google "cpython source code" Commented May 26, 2021 at 1:44
  • 2
    github.com/python/cpython Commented May 26, 2021 at 1:44

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I first heard of cpython.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.