looking for method in python standard library

88 views Asked by At

In my python standard library(Python 2.7), I am looking for specific methods within the modules.

For e.g. if i open re.py, i can see that there are specific methods like findall,search etc. The same is evident when i do dir(re). I can clearly see the above methods.

Similarly,when i do dir(os), there exists a method called system, which i usually call using os.system(cmd_name).

But when i look for this method under os.py, it does not exist. Am i doing something wrong? Please guide

1

There are 1 answers

6
user2357112 On BEST ANSWER

This function is not defined in the os module, so you won't find the definition in os.py. It's imported into os from another module; which module that is will depend on your operating system. You can check:

>>> os.system.__module__
'nt'

Here, you can see that since I'm on Windows, os.system comes from the nt module.


You'll probably find that os.system.__module__ is either 'nt' or 'posix', representing either the nt or posix module. The question then is, where are those modules defined? There's no nt.py or posix.py.

It turns out these modules are implemented in C, in Modules/posixmodule.c. Yes, the nt module is posixmodule.c, and so is the posix module. It's weird.