简体   繁体   中英

Finding which script contains a particular function or class

How can I find out the file path and correct script containing a particular function or class of a module?


Example: Say I want to take a look at the tkinter source code for tkinter.Button() .

I found where the __init__.py file is located for tkinter using:

>>>import tkinter
>>>tkinter.__file__
'C:\\Users\\Simon\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\tkinter\\__init__.py'

I searched through that folder but was unable to locate any function or class named Button . My guess that it is contained inside the run-times and not a .py file but I am not definite and want to be sure of this.

I could not find anything useful in the Python documentation special attributes either.

Any suggestions are welcome but please note that this post is not set only on the tkinter module but most packages or modules, tkinter just being an example.

The tkinter objects are not implemented as Python source code. See the tkinter documentation :

The Tk interface is located in a binary module named _tkinter. This module contains the low-level interface to Tk, and should never be used directly by application programmers. It is usually a shared library (or DLL), but might in some cases be statically linked with the Python interpreter.

See the Tcl/Tk documentation the tkinter library documentation links to. The Tcl/Tk documentation in turn links to https://core.tcl.tk/ , the central hub for development of that project (which is external to Python).

Following links from there you can find the GitHub mirror of the project source code , which includes the Tk source , where you can find the tkButton.c source file ; this is the core button widget implementation.

There is some Python code involved as well , but the class definitions there are mostly there to just pass on commands to the native Tk library.

Any suggestions are welcome but please note that this post is not set only on the >tkinter module but most packages or modules, tkinter just being an example.

This pretty much answers your question (for general cases) How do I get the filepath for a class in Python?

One can also see that Class Button(Widget) is defined in the init , but this is a wrapper as @Martijn Pieters wrote.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM