Pro Dude
Pro Dude

Reputation: 33

How would I view what code is being executed when I use a certain function?

I am wondering what the print function does behind the scenes. How can I view the code that is executed when I use the print function in python, among other functions?

Upvotes: 0

Views: 227

Answers (2)

max
max

Reputation: 29983

Learning how something in the standard library is implemented is a great way to learn about that language.

Unfortunately considerable parts of Python are implemented in C language which makes studying this parts less educational and more difficult.

The modules (the stuff you import) can be found in the filesystem and the Parts are easy to inspect. Try

import os.path
print(os.path.__file__)

This should show you the file where a certain module (for example os.path) is implemented.

To my vague memory print originates from a module called builtins and is implemented in C - but I might be wrong.

Basically it looks like this:

def print(*args):
    sys.stdout.write(''.join(args))

sys.stdout.write calls Operating System write function for the file handle 1 which is defined as stdout or "the screen'.

Upvotes: 2

apridos
apridos

Reputation: 182

you can user Inspect

your code should look like this

import inspect

inspect.getdoc(print)

Edit : not for something being

Upvotes: 1

Related Questions