1

Is it possible to view, how exactly methods work? For example list method count(). I have a list:

li = [3, 5, 6, 7, 3, 3, 3]

When I type

print li.count(3)

The ouptut will be 4. How I can see a code where this magic happens? Command help(list.count) gives short insufficient info:

>>> help(list.count)
Help on method_descriptor:

count(...)
    L.count(value) -> integer -- return number of occurrences of value
goodgrief
  • 378
  • 1
  • 8
  • 23

1 Answers1

1

Most builtins are implemented in C, so you won't be able to see the code. You can however get a detailed help to everything with the "help" function.

help(li.count)

This gives you enough information to really know what things you can do with whatever object you are supplying to the help. What I did when I started out is to write my own functions that emulate the functionality. This gives you a really good grasp of all the things you have to consider. Here is an example of how a count function could look like:

def count(crit, iterable):
    i = 0
    for item in iterable:
        if crit == item:
            i += 1
    return i

As an alternative, a lot of things (like the tkinter module) are written in Python and you can look them up in pythonx.x/Lib/tkinter (replace tkinter with whatever module you want to look at). I hope that answers your question well enough.

Matthias Schreiber
  • 2,347
  • 1
  • 13
  • 20