Reputation: 23322
I'm trying to understand how some built-in Python modules work under the hood.
For example, with the code:
from Tkinter import *
root = Tk()
root.mainloop()
Where is the definition of the function Tk
?
I've been searching through the tkinter source code but cannot find it.
The code several time calls import Tkinter
which is also strange because this is Tkinter
, so why is it importing itself?
Hope someone can help resolve my confusion
Upvotes: 3
Views: 2810
Reputation: 5055
Although cxw's answer is accurate and correct, I would suggest installing IPython, if you find yourself looking through the code of Python modules often. It's a better version of the python console:
$ pip install IPython
...
$ ipython
Then, to see the code of a class / function / module, type its name in and add ??
:
from Tkinter import *
Tk??
Upvotes: 6
Reputation: 17041
class Tk
is in:
Tkinter.py
__init__.py
. In general, when you import Foo
, if Foo
is a module implemented using Python code, and there is a Foo/__init__.py
in Python's search path, that file runs. Some more information here, plus official docs on modules for Python 2 or Python 3. That may not be the case for modules built in to the Python interpreter, such as 2.7's Tkinter
.
Tkinter
specificsTkinter
is a Python module and tkinter
(lowercase) is a C module that Tkinter
uses (source).tkinter
(lowercase) is a Python module and _tkinter
(with underscore) is the C module that tkinter
uses (source).Upvotes: 5