Linux iad1-shared-b7-18 6.6.49-grsec-jammy+ #10 SMP Thu Sep 12 23:23:08 UTC 2024 x86_64
Apache
: 67.205.6.31 | : 216.73.216.47
Cant Read [ /etc/named.conf ]
8.2.29
fernandoquevedo
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python3.10 /
html /
library /
[ HOME SHELL ]
Name
Size
Permission
Action
2to3.html
70.96
KB
-rw-r--r--
__future__.html
20.75
KB
-rw-r--r--
__main__.html
44.87
KB
-rw-r--r--
_thread.html
30.39
KB
-rw-r--r--
abc.html
46.87
KB
-rw-r--r--
aifc.html
35.56
KB
-rw-r--r--
allos.html
34.33
KB
-rw-r--r--
archiving.html
17
KB
-rw-r--r--
argparse.html
300.89
KB
-rw-r--r--
array.html
37.28
KB
-rw-r--r--
ast.html
293.08
KB
-rw-r--r--
asynchat.html
37.41
KB
-rw-r--r--
asyncio-api-index.html
27.68
KB
-rw-r--r--
asyncio-dev.html
36.1
KB
-rw-r--r--
asyncio-eventloop.html
204.43
KB
-rw-r--r--
asyncio-exceptions.html
17.11
KB
-rw-r--r--
asyncio-future.html
38.23
KB
-rw-r--r--
asyncio-llapi-index.html
61.97
KB
-rw-r--r--
asyncio-platforms.html
21.77
KB
-rw-r--r--
asyncio-policy.html
39.56
KB
-rw-r--r--
asyncio-protocol.html
118.33
KB
-rw-r--r--
asyncio-queue.html
31.86
KB
-rw-r--r--
asyncio-stream.html
69.98
KB
-rw-r--r--
asyncio-subprocess.html
49.25
KB
-rw-r--r--
asyncio-sync.html
42.79
KB
-rw-r--r--
asyncio-task.html
109.14
KB
-rw-r--r--
asyncio.html
17.31
KB
-rw-r--r--
asyncore.html
49.36
KB
-rw-r--r--
atexit.html
22.87
KB
-rw-r--r--
audioop.html
46
KB
-rw-r--r--
audit_events.html
72.85
KB
-rw-r--r--
base64.html
48.09
KB
-rw-r--r--
bdb.html
66
KB
-rw-r--r--
binary.html
15.97
KB
-rw-r--r--
binascii.html
36.3
KB
-rw-r--r--
binhex.html
15.97
KB
-rw-r--r--
bisect.html
43.38
KB
-rw-r--r--
builtins.html
14.87
KB
-rw-r--r--
bz2.html
50.68
KB
-rw-r--r--
calendar.html
69.35
KB
-rw-r--r--
cgi.html
59.95
KB
-rw-r--r--
cgitb.html
19.96
KB
-rw-r--r--
chunk.html
22.21
KB
-rw-r--r--
cmath.html
43.89
KB
-rw-r--r--
cmd.html
50.42
KB
-rw-r--r--
code.html
35.38
KB
-rw-r--r--
codecs.html
157.96
KB
-rw-r--r--
codeop.html
18.26
KB
-rw-r--r--
collections.abc.html
79.17
KB
-rw-r--r--
collections.html
189.43
KB
-rw-r--r--
colorsys.html
17.31
KB
-rw-r--r--
compileall.html
50.68
KB
-rw-r--r--
concurrency.html
24.15
KB
-rw-r--r--
concurrent.futures.html
79.03
KB
-rw-r--r--
concurrent.html
11.56
KB
-rw-r--r--
configparser.html
159.4
KB
-rw-r--r--
constants.html
22.41
KB
-rw-r--r--
contextlib.html
117.2
KB
-rw-r--r--
contextvars.html
38.38
KB
-rw-r--r--
copy.html
17.92
KB
-rw-r--r--
copyreg.html
18.63
KB
-rw-r--r--
crypt.html
28.29
KB
-rw-r--r--
crypto.html
13.93
KB
-rw-r--r--
csv.html
82.89
KB
-rw-r--r--
ctypes.html
295.49
KB
-rw-r--r--
curses.ascii.html
32.04
KB
-rw-r--r--
curses.html
221.24
KB
-rw-r--r--
curses.panel.html
22.16
KB
-rw-r--r--
custominterp.html
11.85
KB
-rw-r--r--
dataclasses.html
113.16
KB
-rw-r--r--
datatypes.html
31.82
KB
-rw-r--r--
datetime.html
374.92
KB
-rw-r--r--
dbm.html
51.16
KB
-rw-r--r--
debug.html
17
KB
-rw-r--r--
decimal.html
270.12
KB
-rw-r--r--
development.html
32.34
KB
-rw-r--r--
devmode.html
31.72
KB
-rw-r--r--
dialog.html
43.03
KB
-rw-r--r--
difflib.html
111.99
KB
-rw-r--r--
dis.html
136.07
KB
-rw-r--r--
distribution.html
13.67
KB
-rw-r--r--
distutils.html
14.73
KB
-rw-r--r--
doctest.html
199.32
KB
-rw-r--r--
email.charset.html
31.92
KB
-rw-r--r--
email.compat32-message.html
97.74
KB
-rw-r--r--
email.contentmanager.html
37.02
KB
-rw-r--r--
email.encoders.html
18.47
KB
-rw-r--r--
email.errors.html
24.45
KB
-rw-r--r--
email.examples.html
60.49
KB
-rw-r--r--
email.generator.html
48.79
KB
-rw-r--r--
email.header.html
37.61
KB
-rw-r--r--
email.headerregistry.html
66.26
KB
-rw-r--r--
email.html
30.75
KB
-rw-r--r--
email.iterators.html
18.23
KB
-rw-r--r--
email.message.html
99.75
KB
-rw-r--r--
email.mime.html
44.89
KB
-rw-r--r--
email.parser.html
57.39
KB
-rw-r--r--
email.policy.html
84.24
KB
-rw-r--r--
email.utils.html
42.57
KB
-rw-r--r--
ensurepip.html
23.9
KB
-rw-r--r--
enum.html
151
KB
-rw-r--r--
errno.html
60.17
KB
-rw-r--r--
exceptions.html
108.44
KB
-rw-r--r--
faulthandler.html
31.5
KB
-rw-r--r--
fcntl.html
36.67
KB
-rw-r--r--
filecmp.html
32.08
KB
-rw-r--r--
fileformats.html
13.52
KB
-rw-r--r--
fileinput.html
40.37
KB
-rw-r--r--
filesys.html
16.78
KB
-rw-r--r--
fnmatch.html
21.02
KB
-rw-r--r--
fractions.html
35.29
KB
-rw-r--r--
frameworks.html
16.15
KB
-rw-r--r--
ftplib.html
71.97
KB
-rw-r--r--
functional.html
12.19
KB
-rw-r--r--
functions.html
260.33
KB
-rw-r--r--
functools.html
92.96
KB
-rw-r--r--
gc.html
40.97
KB
-rw-r--r--
getopt.html
29.4
KB
-rw-r--r--
getpass.html
16.01
KB
-rw-r--r--
gettext.html
98.02
KB
-rw-r--r--
glob.html
27.2
KB
-rw-r--r--
graphlib.html
34.82
KB
-rw-r--r--
grp.html
16.03
KB
-rw-r--r--
gzip.html
45.65
KB
-rw-r--r--
hashlib.html
93.19
KB
-rw-r--r--
heapq.html
42.43
KB
-rw-r--r--
hmac.html
24.74
KB
-rw-r--r--
html.entities.html
14.91
KB
-rw-r--r--
html.html
14.81
KB
-rw-r--r--
html.parser.html
47.7
KB
-rw-r--r--
http.client.html
85.63
KB
-rw-r--r--
http.cookiejar.html
113.94
KB
-rw-r--r--
http.cookies.html
47.64
KB
-rw-r--r--
http.html
45.52
KB
-rw-r--r--
http.server.html
73.05
KB
-rw-r--r--
i18n.html
13.44
KB
-rw-r--r--
idle.html
77.18
KB
-rw-r--r--
imaplib.html
87.08
KB
-rw-r--r--
imghdr.html
17.94
KB
-rw-r--r--
imp.html
54.32
KB
-rw-r--r--
importlib.html
239.96
KB
-rw-r--r--
importlib.metadata.html
43.14
KB
-rw-r--r--
index.html
75.06
KB
-rw-r--r--
inspect.html
168.74
KB
-rw-r--r--
internet.html
29.89
KB
-rw-r--r--
intro.html
13.81
KB
-rw-r--r--
io.html
154.15
KB
-rw-r--r--
ipaddress.html
137.75
KB
-rw-r--r--
ipc.html
12.6
KB
-rw-r--r--
itertools.html
153.27
KB
-rw-r--r--
json.html
98.9
KB
-rw-r--r--
keyword.html
14.88
KB
-rw-r--r--
language.html
16.71
KB
-rw-r--r--
linecache.html
18.09
KB
-rw-r--r--
locale.html
77.35
KB
-rw-r--r--
logging.config.html
100.3
KB
-rw-r--r--
logging.handlers.html
146.28
KB
-rw-r--r--
logging.html
169.67
KB
-rw-r--r--
lzma.html
66.89
KB
-rw-r--r--
mailbox.html
176.34
KB
-rw-r--r--
mailcap.html
20.86
KB
-rw-r--r--
markup.html
22.31
KB
-rw-r--r--
marshal.html
24.43
KB
-rw-r--r--
math.html
85.82
KB
-rw-r--r--
mimetypes.html
40.27
KB
-rw-r--r--
mm.html
11.16
KB
-rw-r--r--
mmap.html
55.53
KB
-rw-r--r--
modulefinder.html
23.67
KB
-rw-r--r--
modules.html
15.88
KB
-rw-r--r--
msilib.html
78.38
KB
-rw-r--r--
msvcrt.html
29.22
KB
-rw-r--r--
multiprocessing.html
405.24
KB
-rw-r--r--
multiprocessing.shared_memory....
60
KB
-rw-r--r--
netdata.html
20.24
KB
-rw-r--r--
netrc.html
20.39
KB
-rw-r--r--
nis.html
16.76
KB
-rw-r--r--
nntplib.html
84.93
KB
-rw-r--r--
numbers.html
45.67
KB
-rw-r--r--
numeric.html
19.29
KB
-rw-r--r--
operator.html
107.32
KB
-rw-r--r--
optparse.html
256.96
KB
-rw-r--r--
os.html
594.39
KB
-rw-r--r--
os.path.html
65.54
KB
-rw-r--r--
ossaudiodev.html
56.35
KB
-rw-r--r--
pathlib.html
170.01
KB
-rw-r--r--
pdb.html
68.67
KB
-rw-r--r--
persistence.html
19.13
KB
-rw-r--r--
pickle.html
154.45
KB
-rw-r--r--
pickletools.html
22.92
KB
-rw-r--r--
pipes.html
21.7
KB
-rw-r--r--
pkgutil.html
42.76
KB
-rw-r--r--
platform.html
45.08
KB
-rw-r--r--
plistlib.html
30.06
KB
-rw-r--r--
poplib.html
41.42
KB
-rw-r--r--
posix.html
20.96
KB
-rw-r--r--
pprint.html
55.35
KB
-rw-r--r--
profile.html
84.63
KB
-rw-r--r--
pty.html
23.7
KB
-rw-r--r--
pwd.html
16.84
KB
-rw-r--r--
py_compile.html
29
KB
-rw-r--r--
pyclbr.html
24.06
KB
-rw-r--r--
pydoc.html
19.59
KB
-rw-r--r--
pyexpat.html
104.38
KB
-rw-r--r--
python.html
22.46
KB
-rw-r--r--
queue.html
43.39
KB
-rw-r--r--
quopri.html
18.64
KB
-rw-r--r--
random.html
86.31
KB
-rw-r--r--
re.html
214.24
KB
-rw-r--r--
readline.html
50.19
KB
-rw-r--r--
reprlib.html
30.85
KB
-rw-r--r--
resource.html
48.26
KB
-rw-r--r--
rlcompleter.html
18.06
KB
-rw-r--r--
runpy.html
30.99
KB
-rw-r--r--
sched.html
27.04
KB
-rw-r--r--
secrets.html
30.2
KB
-rw-r--r--
security_warnings.html
16.21
KB
-rw-r--r--
select.html
74.58
KB
-rw-r--r--
selectors.html
41.91
KB
-rw-r--r--
shelve.html
36.54
KB
-rw-r--r--
shlex.html
61.03
KB
-rw-r--r--
shutil.html
100.13
KB
-rw-r--r--
signal.html
95.39
KB
-rw-r--r--
site.html
38.16
KB
-rw-r--r--
smtpd.html
44.09
KB
-rw-r--r--
smtplib.html
81.4
KB
-rw-r--r--
sndhdr.html
16.4
KB
-rw-r--r--
socket.html
256.95
KB
-rw-r--r--
socketserver.html
91.95
KB
-rw-r--r--
spwd.html
16.71
KB
-rw-r--r--
sqlite3.html
225.51
KB
-rw-r--r--
ssl.html
328.83
KB
-rw-r--r--
stat.html
57.79
KB
-rw-r--r--
statistics.html
123.22
KB
-rw-r--r--
stdtypes.html
615.43
KB
-rw-r--r--
string.html
108.03
KB
-rw-r--r--
stringprep.html
24.94
KB
-rw-r--r--
struct.html
70.58
KB
-rw-r--r--
subprocess.html
199.03
KB
-rw-r--r--
sunau.html
39.84
KB
-rw-r--r--
superseded.html
26.21
KB
-rw-r--r--
symtable.html
33.26
KB
-rw-r--r--
sys.html
198.83
KB
-rw-r--r--
sysconfig.html
39.81
KB
-rw-r--r--
syslog.html
27.36
KB
-rw-r--r--
tabnanny.html
16.18
KB
-rw-r--r--
tarfile.html
161.92
KB
-rw-r--r--
telnetlib.html
38.21
KB
-rw-r--r--
tempfile.html
58.39
KB
-rw-r--r--
termios.html
22.86
KB
-rw-r--r--
test.html
222.85
KB
-rw-r--r--
text.html
16.8
KB
-rw-r--r--
textwrap.html
48.83
KB
-rw-r--r--
threading.html
128.33
KB
-rw-r--r--
time.html
107.46
KB
-rw-r--r--
timeit.html
52.63
KB
-rw-r--r--
tk.html
28.08
KB
-rw-r--r--
tkinter.colorchooser.html
14.49
KB
-rw-r--r--
tkinter.dnd.html
17.09
KB
-rw-r--r--
tkinter.font.html
22.21
KB
-rw-r--r--
tkinter.html
106.85
KB
-rw-r--r--
tkinter.messagebox.html
20.09
KB
-rw-r--r--
tkinter.scrolledtext.html
14.52
KB
-rw-r--r--
tkinter.tix.html
59.69
KB
-rw-r--r--
tkinter.ttk.html
138.37
KB
-rw-r--r--
token.html
45.73
KB
-rw-r--r--
tokenize.html
39.86
KB
-rw-r--r--
trace.html
38.85
KB
-rw-r--r--
traceback.html
75.61
KB
-rw-r--r--
tracemalloc.html
122.33
KB
-rw-r--r--
tty.html
14.46
KB
-rw-r--r--
turtle.html
271.71
KB
-rw-r--r--
types.html
64.85
KB
-rw-r--r--
typing.html
315.69
KB
-rw-r--r--
unicodedata.html
27.3
KB
-rw-r--r--
unittest.html
310.01
KB
-rw-r--r--
unittest.mock-examples.html
177.75
KB
-rw-r--r--
unittest.mock.html
363.51
KB
-rw-r--r--
unix.html
13.46
KB
-rw-r--r--
urllib.error.html
18.56
KB
-rw-r--r--
urllib.html
12.92
KB
-rw-r--r--
urllib.parse.html
104.65
KB
-rw-r--r--
urllib.request.html
208.87
KB
-rw-r--r--
urllib.robotparser.html
22.63
KB
-rw-r--r--
uu.html
18.2
KB
-rw-r--r--
uuid.html
42.88
KB
-rw-r--r--
venv.html
90.43
KB
-rw-r--r--
warnings.html
69.13
KB
-rw-r--r--
wave.html
36.12
KB
-rw-r--r--
weakref.html
73.36
KB
-rw-r--r--
webbrowser.html
33.98
KB
-rw-r--r--
windows.html
12.32
KB
-rw-r--r--
winreg.html
89.48
KB
-rw-r--r--
winsound.html
26.3
KB
-rw-r--r--
wsgiref.html
111.52
KB
-rw-r--r--
xdrlib.html
42.07
KB
-rw-r--r--
xml.dom.html
114.66
KB
-rw-r--r--
xml.dom.minidom.html
49.6
KB
-rw-r--r--
xml.dom.pulldom.html
30.06
KB
-rw-r--r--
xml.etree.elementtree.html
186.49
KB
-rw-r--r--
xml.html
22.34
KB
-rw-r--r--
xml.sax.handler.html
55.78
KB
-rw-r--r--
xml.sax.html
31.74
KB
-rw-r--r--
xml.sax.reader.html
56.23
KB
-rw-r--r--
xml.sax.utils.html
22.28
KB
-rw-r--r--
xmlrpc.client.html
79.73
KB
-rw-r--r--
xmlrpc.html
11.9
KB
-rw-r--r--
xmlrpc.server.html
63.56
KB
-rw-r--r--
zipapp.html
52.45
KB
-rw-r--r--
zipfile.html
116.59
KB
-rw-r--r--
zipimport.html
33.76
KB
-rw-r--r--
zlib.html
42.29
KB
-rw-r--r--
zoneinfo.html
57.25
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : inspect.html
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" /> <title>inspect — Inspect live objects — Python 3.10.12 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" type="text/css" href="../_static/pygments.css" /> <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?2022.1" /> <script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script> <script src="../_static/jquery.js"></script> <script src="../_static/underscore.js"></script> <script src="../_static/doctools.js"></script> <script src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 3.10.12 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="index" title="Index" href="../genindex.html" /> <link rel="search" title="Search" href="../search.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="next" title="site — Site-specific configuration hook" href="site.html" /> <link rel="prev" title="gc — Garbage Collector interface" href="gc.html" /> <link rel="canonical" href="file:///usr/share/doc/python3.10/html/library/inspect.html" /> <style> @media only screen { table.full-width-table { width: 100%; } } </style> <link rel="shortcut icon" type="image/png" href="../_static/py.svg" /> <script type="text/javascript" src="../_static/copybutton.js"></script> <script type="text/javascript" src="../_static/menu.js"></script> </head> <body> <div class="mobile-nav"> <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation" aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" /> <label for="menuToggler" class="toggler__label"> <span></span> </label> <nav class="nav-content" role="navigation"> <a href="https://www.python.org/" class="nav-logo"> <img src="../_static/py.svg" alt="Logo"/> </a> <div class="version_switcher_placeholder"></div> <form role="search" class="search" action="../search.html" method="get"> <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon"> <path fill-rule="nonzero" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path> </svg> <input type="text" name="q" aria-label="Quick search"/> <input type="submit" value="Go"/> </form> </nav> <div class="menu-wrapper"> <nav class="menu" role="navigation" aria-label="main navigation"> <div class="language_switcher_placeholder"></div> <h3><a href="../contents.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code> — Inspect live objects</a><ul> <li><a class="reference internal" href="#types-and-members">Types and members</a></li> <li><a class="reference internal" href="#retrieving-source-code">Retrieving source code</a></li> <li><a class="reference internal" href="#introspecting-callables-with-the-signature-object">Introspecting callables with the Signature object</a></li> <li><a class="reference internal" href="#classes-and-functions">Classes and functions</a></li> <li><a class="reference internal" href="#the-interpreter-stack">The interpreter stack</a></li> <li><a class="reference internal" href="#fetching-attributes-statically">Fetching attributes statically</a></li> <li><a class="reference internal" href="#current-state-of-generators-and-coroutines">Current State of Generators and Coroutines</a></li> <li><a class="reference internal" href="#code-objects-bit-flags">Code Objects Bit Flags</a></li> <li><a class="reference internal" href="#command-line-interface">Command Line Interface</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="gc.html" title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code> — Garbage Collector interface</a></p> <h4>Next topic</h4> <p class="topless"><a href="site.html" title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code> — Site-specific configuration hook</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li> <a href="https://github.com/python/cpython/blob/3.10/Doc/library/inspect.rst" rel="nofollow">Show Source </a> </li> </ul> </div> </nav> </div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="site.html" title="site — Site-specific configuration hook" accesskey="N">next</a> |</li> <li class="right" > <a href="gc.html" title="gc — Garbage Collector interface" accesskey="P">previous</a> |</li> <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li class="switchers"> <div class="language_switcher_placeholder"></div> <div class="version_switcher_placeholder"></div> </li> <li> </li> <li id="cpython-language-and-version"> <a href="../index.html">3.10.12 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="python.html" accesskey="U">Python Runtime Services</a> »</li> <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code> — Inspect live objects</a></li> <li class="right"> <div class="inline-search" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" aria-label="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> | </li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <section id="module-inspect"> <span id="inspect-inspect-live-objects"></span><h1><a class="reference internal" href="#module-inspect" title="inspect: Extract information and source code from live objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code></a> — Inspect live objects<a class="headerlink" href="#module-inspect" title="Permalink to this headline">¶</a></h1> <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.10/Lib/inspect.py">Lib/inspect.py</a></p> <hr class="docutils" /> <p>The <a class="reference internal" href="#module-inspect" title="inspect: Extract information and source code from live objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code></a> module provides several useful functions to help get information about live objects such as modules, classes, methods, functions, tracebacks, frame objects, and code objects. For example, it can help you examine the contents of a class, retrieve the source code of a method, extract and format the argument list for a function, or get all the information you need to display a detailed traceback.</p> <p>There are four main kinds of services provided by this module: type checking, getting source code, inspecting classes and functions, and examining the interpreter stack.</p> <section id="types-and-members"> <span id="inspect-types"></span><h2>Types and members<a class="headerlink" href="#types-and-members" title="Permalink to this headline">¶</a></h2> <p>The <a class="reference internal" href="#inspect.getmembers" title="inspect.getmembers"><code class="xref py py-func docutils literal notranslate"><span class="pre">getmembers()</span></code></a> function retrieves the members of an object such as a class or module. The functions whose names begin with “is” are mainly provided as convenient choices for the second argument to <a class="reference internal" href="#inspect.getmembers" title="inspect.getmembers"><code class="xref py py-func docutils literal notranslate"><span class="pre">getmembers()</span></code></a>. They also help you determine when you can expect to find the following special attributes:</p> <table class="docutils align-default"> <colgroup> <col style="width: 19%" /> <col style="width: 33%" /> <col style="width: 47%" /> </colgroup> <thead> <tr class="row-odd"><th class="head"><p>Type</p></th> <th class="head"><p>Attribute</p></th> <th class="head"><p>Description</p></th> </tr> </thead> <tbody> <tr class="row-even"><td><p>module</p></td> <td><p>__doc__</p></td> <td><p>documentation string</p></td> </tr> <tr class="row-odd"><td></td> <td><p>__file__</p></td> <td><p>filename (missing for built-in modules)</p></td> </tr> <tr class="row-even"><td><p>class</p></td> <td><p>__doc__</p></td> <td><p>documentation string</p></td> </tr> <tr class="row-odd"><td></td> <td><p>__name__</p></td> <td><p>name with which this class was defined</p></td> </tr> <tr class="row-even"><td></td> <td><p>__qualname__</p></td> <td><p>qualified name</p></td> </tr> <tr class="row-odd"><td></td> <td><p>__module__</p></td> <td><p>name of module in which this class was defined</p></td> </tr> <tr class="row-even"><td><p>method</p></td> <td><p>__doc__</p></td> <td><p>documentation string</p></td> </tr> <tr class="row-odd"><td></td> <td><p>__name__</p></td> <td><p>name with which this method was defined</p></td> </tr> <tr class="row-even"><td></td> <td><p>__qualname__</p></td> <td><p>qualified name</p></td> </tr> <tr class="row-odd"><td></td> <td><p>__func__</p></td> <td><p>function object containing implementation of method</p></td> </tr> <tr class="row-even"><td></td> <td><p>__self__</p></td> <td><p>instance to which this method is bound, or <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td> </tr> <tr class="row-odd"><td></td> <td><p>__module__</p></td> <td><p>name of module in which this method was defined</p></td> </tr> <tr class="row-even"><td><p>function</p></td> <td><p>__doc__</p></td> <td><p>documentation string</p></td> </tr> <tr class="row-odd"><td></td> <td><p>__name__</p></td> <td><p>name with which this function was defined</p></td> </tr> <tr class="row-even"><td></td> <td><p>__qualname__</p></td> <td><p>qualified name</p></td> </tr> <tr class="row-odd"><td></td> <td><p>__code__</p></td> <td><p>code object containing compiled function <a class="reference internal" href="../glossary.html#term-bytecode"><span class="xref std std-term">bytecode</span></a></p></td> </tr> <tr class="row-even"><td></td> <td><p>__defaults__</p></td> <td><p>tuple of any default values for positional or keyword parameters</p></td> </tr> <tr class="row-odd"><td></td> <td><p>__kwdefaults__</p></td> <td><p>mapping of any default values for keyword-only parameters</p></td> </tr> <tr class="row-even"><td></td> <td><p>__globals__</p></td> <td><p>global namespace in which this function was defined</p></td> </tr> <tr class="row-odd"><td></td> <td><p>__builtins__</p></td> <td><p>builtins namespace</p></td> </tr> <tr class="row-even"><td></td> <td><p>__annotations__</p></td> <td><p>mapping of parameters names to annotations; <code class="docutils literal notranslate"><span class="pre">"return"</span></code> key is reserved for return annotations.</p></td> </tr> <tr class="row-odd"><td></td> <td><p>__module__</p></td> <td><p>name of module in which this function was defined</p></td> </tr> <tr class="row-even"><td><p>traceback</p></td> <td><p>tb_frame</p></td> <td><p>frame object at this level</p></td> </tr> <tr class="row-odd"><td></td> <td><p>tb_lasti</p></td> <td><p>index of last attempted instruction in bytecode</p></td> </tr> <tr class="row-even"><td></td> <td><p>tb_lineno</p></td> <td><p>current line number in Python source code</p></td> </tr> <tr class="row-odd"><td></td> <td><p>tb_next</p></td> <td><p>next inner traceback object (called by this level)</p></td> </tr> <tr class="row-even"><td><p>frame</p></td> <td><p>f_back</p></td> <td><p>next outer frame object (this frame’s caller)</p></td> </tr> <tr class="row-odd"><td></td> <td><p>f_builtins</p></td> <td><p>builtins namespace seen by this frame</p></td> </tr> <tr class="row-even"><td></td> <td><p>f_code</p></td> <td><p>code object being executed in this frame</p></td> </tr> <tr class="row-odd"><td></td> <td><p>f_globals</p></td> <td><p>global namespace seen by this frame</p></td> </tr> <tr class="row-even"><td></td> <td><p>f_lasti</p></td> <td><p>index of last attempted instruction in bytecode</p></td> </tr> <tr class="row-odd"><td></td> <td><p>f_lineno</p></td> <td><p>current line number in Python source code</p></td> </tr> <tr class="row-even"><td></td> <td><p>f_locals</p></td> <td><p>local namespace seen by this frame</p></td> </tr> <tr class="row-odd"><td></td> <td><p>f_trace</p></td> <td><p>tracing function for this frame, or <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td> </tr> <tr class="row-even"><td><p>code</p></td> <td><p>co_argcount</p></td> <td><p>number of arguments (not including keyword only arguments, * or ** args)</p></td> </tr> <tr class="row-odd"><td></td> <td><p>co_code</p></td> <td><p>string of raw compiled bytecode</p></td> </tr> <tr class="row-even"><td></td> <td><p>co_cellvars</p></td> <td><p>tuple of names of cell variables (referenced by containing scopes)</p></td> </tr> <tr class="row-odd"><td></td> <td><p>co_consts</p></td> <td><p>tuple of constants used in the bytecode</p></td> </tr> <tr class="row-even"><td></td> <td><p>co_filename</p></td> <td><p>name of file in which this code object was created</p></td> </tr> <tr class="row-odd"><td></td> <td><p>co_firstlineno</p></td> <td><p>number of first line in Python source code</p></td> </tr> <tr class="row-even"><td></td> <td><p>co_flags</p></td> <td><p>bitmap of <code class="docutils literal notranslate"><span class="pre">CO_*</span></code> flags, read more <a class="reference internal" href="#inspect-module-co-flags"><span class="std std-ref">here</span></a></p></td> </tr> <tr class="row-odd"><td></td> <td><p>co_lnotab</p></td> <td><p>encoded mapping of line numbers to bytecode indices</p></td> </tr> <tr class="row-even"><td></td> <td><p>co_freevars</p></td> <td><p>tuple of names of free variables (referenced via a function’s closure)</p></td> </tr> <tr class="row-odd"><td></td> <td><p>co_posonlyargcount</p></td> <td><p>number of positional only arguments</p></td> </tr> <tr class="row-even"><td></td> <td><p>co_kwonlyargcount</p></td> <td><p>number of keyword only arguments (not including ** arg)</p></td> </tr> <tr class="row-odd"><td></td> <td><p>co_name</p></td> <td><p>name with which this code object was defined</p></td> </tr> <tr class="row-even"><td></td> <td><p>co_names</p></td> <td><p>tuple of names other than arguments and function locals</p></td> </tr> <tr class="row-odd"><td></td> <td><p>co_nlocals</p></td> <td><p>number of local variables</p></td> </tr> <tr class="row-even"><td></td> <td><p>co_stacksize</p></td> <td><p>virtual machine stack space required</p></td> </tr> <tr class="row-odd"><td></td> <td><p>co_varnames</p></td> <td><p>tuple of names of arguments and local variables</p></td> </tr> <tr class="row-even"><td><p>generator</p></td> <td><p>__name__</p></td> <td><p>name</p></td> </tr> <tr class="row-odd"><td></td> <td><p>__qualname__</p></td> <td><p>qualified name</p></td> </tr> <tr class="row-even"><td></td> <td><p>gi_frame</p></td> <td><p>frame</p></td> </tr> <tr class="row-odd"><td></td> <td><p>gi_running</p></td> <td><p>is the generator running?</p></td> </tr> <tr class="row-even"><td></td> <td><p>gi_code</p></td> <td><p>code</p></td> </tr> <tr class="row-odd"><td></td> <td><p>gi_yieldfrom</p></td> <td><p>object being iterated by <code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span></code>, or <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td> </tr> <tr class="row-even"><td><p>coroutine</p></td> <td><p>__name__</p></td> <td><p>name</p></td> </tr> <tr class="row-odd"><td></td> <td><p>__qualname__</p></td> <td><p>qualified name</p></td> </tr> <tr class="row-even"><td></td> <td><p>cr_await</p></td> <td><p>object being awaited on, or <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td> </tr> <tr class="row-odd"><td></td> <td><p>cr_frame</p></td> <td><p>frame</p></td> </tr> <tr class="row-even"><td></td> <td><p>cr_running</p></td> <td><p>is the coroutine running?</p></td> </tr> <tr class="row-odd"><td></td> <td><p>cr_code</p></td> <td><p>code</p></td> </tr> <tr class="row-even"><td></td> <td><p>cr_origin</p></td> <td><p>where coroutine was created, or <code class="docutils literal notranslate"><span class="pre">None</span></code>. See <a class="reference internal" href="sys.html#sys.set_coroutine_origin_tracking_depth" title="sys.set_coroutine_origin_tracking_depth"><code class="xref py py-func docutils literal notranslate"><span class="pre">sys.set_coroutine_origin_tracking_depth()</span></code></a></p></td> </tr> <tr class="row-odd"><td><p>builtin</p></td> <td><p>__doc__</p></td> <td><p>documentation string</p></td> </tr> <tr class="row-even"><td></td> <td><p>__name__</p></td> <td><p>original name of this function or method</p></td> </tr> <tr class="row-odd"><td></td> <td><p>__qualname__</p></td> <td><p>qualified name</p></td> </tr> <tr class="row-even"><td></td> <td><p>__self__</p></td> <td><p>instance to which a method is bound, or <code class="docutils literal notranslate"><span class="pre">None</span></code></p></td> </tr> </tbody> </table> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>Add <code class="docutils literal notranslate"><span class="pre">__qualname__</span></code> and <code class="docutils literal notranslate"><span class="pre">gi_yieldfrom</span></code> attributes to generators.</p> <p>The <code class="docutils literal notranslate"><span class="pre">__name__</span></code> attribute of generators is now set from the function name, instead of the code name, and it can now be modified.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>Add <code class="docutils literal notranslate"><span class="pre">cr_origin</span></code> attribute to coroutines.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.10: </span>Add <code class="docutils literal notranslate"><span class="pre">__builtins__</span></code> attribute to functions.</p> </div> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getmembers"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getmembers</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">predicate</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getmembers" title="Permalink to this definition">¶</a></dt> <dd><p>Return all the members of an object in a list of <code class="docutils literal notranslate"><span class="pre">(name,</span> <span class="pre">value)</span></code> pairs sorted by name. If the optional <em>predicate</em> argument—which will be called with the <code class="docutils literal notranslate"><span class="pre">value</span></code> object of each member—is supplied, only members for which the predicate returns a true value are included.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p><a class="reference internal" href="#inspect.getmembers" title="inspect.getmembers"><code class="xref py py-func docutils literal notranslate"><span class="pre">getmembers()</span></code></a> will only return class attributes defined in the metaclass when the argument is a class and those attributes have been listed in the metaclass’ custom <code class="xref py py-meth docutils literal notranslate"><span class="pre">__dir__()</span></code>.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getmodulename"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getmodulename</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">path</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getmodulename" title="Permalink to this definition">¶</a></dt> <dd><p>Return the name of the module named by the file <em>path</em>, without including the names of enclosing packages. The file extension is checked against all of the entries in <a class="reference internal" href="importlib.html#importlib.machinery.all_suffixes" title="importlib.machinery.all_suffixes"><code class="xref py py-func docutils literal notranslate"><span class="pre">importlib.machinery.all_suffixes()</span></code></a>. If it matches, the final path component is returned with the extension removed. Otherwise, <code class="docutils literal notranslate"><span class="pre">None</span></code> is returned.</p> <p>Note that this function <em>only</em> returns a meaningful name for actual Python modules - paths that potentially refer to Python packages will still return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span>The function is based directly on <a class="reference internal" href="importlib.html#module-importlib" title="importlib: The implementation of the import machinery."><code class="xref py py-mod docutils literal notranslate"><span class="pre">importlib</span></code></a>.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.ismodule"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">ismodule</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.ismodule" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a module.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.isclass"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isclass</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isclass" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a class, whether built-in or created in Python code.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.ismethod"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">ismethod</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.ismethod" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a bound method written in Python.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.isfunction"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isfunction</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isfunction" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a Python function, which includes functions created by a <a class="reference internal" href="../glossary.html#term-lambda"><span class="xref std std-term">lambda</span></a> expression.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.isgeneratorfunction"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isgeneratorfunction</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isgeneratorfunction" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a Python generator function.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Functions wrapped in <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> now return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the wrapped function is a Python generator function.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.isgenerator"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isgenerator</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isgenerator" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a generator.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.iscoroutinefunction"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">iscoroutinefunction</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.iscoroutinefunction" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a <a class="reference internal" href="../glossary.html#term-coroutine-function"><span class="xref std std-term">coroutine function</span></a> (a function defined with an <a class="reference internal" href="../reference/compound_stmts.html#async-def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code></a> syntax).</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Functions wrapped in <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> now return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the wrapped function is a <a class="reference internal" href="../glossary.html#term-coroutine-function"><span class="xref std std-term">coroutine function</span></a>.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.iscoroutine"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">iscoroutine</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.iscoroutine" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a <a class="reference internal" href="../glossary.html#term-coroutine"><span class="xref std std-term">coroutine</span></a> created by an <a class="reference internal" href="../reference/compound_stmts.html#async-def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code></a> function.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.isawaitable"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isawaitable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isawaitable" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object can be used in <a class="reference internal" href="../reference/expressions.html#await"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">await</span></code></a> expression.</p> <p>Can also be used to distinguish generator-based coroutines from regular generators:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">gen</span><span class="p">():</span> <span class="k">yield</span> <span class="nd">@types</span><span class="o">.</span><span class="n">coroutine</span> <span class="k">def</span> <span class="nf">gen_coro</span><span class="p">():</span> <span class="k">yield</span> <span class="k">assert</span> <span class="ow">not</span> <span class="n">isawaitable</span><span class="p">(</span><span class="n">gen</span><span class="p">())</span> <span class="k">assert</span> <span class="n">isawaitable</span><span class="p">(</span><span class="n">gen_coro</span><span class="p">())</span> </pre></div> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.isasyncgenfunction"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isasyncgenfunction</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isasyncgenfunction" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is an <a class="reference internal" href="../glossary.html#term-asynchronous-generator"><span class="xref std std-term">asynchronous generator</span></a> function, for example:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">async</span> <span class="k">def</span> <span class="nf">agen</span><span class="p">():</span> <span class="gp">... </span> <span class="k">yield</span> <span class="mi">1</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">inspect</span><span class="o">.</span><span class="n">isasyncgenfunction</span><span class="p">(</span><span class="n">agen</span><span class="p">)</span> <span class="go">True</span> </pre></div> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.8: </span>Functions wrapped in <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> now return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the wrapped function is a <a class="reference internal" href="../glossary.html#term-asynchronous-generator"><span class="xref std std-term">asynchronous generator</span></a> function.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.isasyncgen"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isasyncgen</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isasyncgen" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is an <a class="reference internal" href="../glossary.html#term-asynchronous-generator-iterator"><span class="xref std std-term">asynchronous generator iterator</span></a> created by an <a class="reference internal" href="../glossary.html#term-asynchronous-generator"><span class="xref std std-term">asynchronous generator</span></a> function.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.istraceback"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">istraceback</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.istraceback" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a traceback.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.isframe"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isframe</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isframe" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a frame.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.iscode"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">iscode</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.iscode" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a code.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.isbuiltin"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isbuiltin</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isbuiltin" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a built-in function or a bound built-in method.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.isroutine"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isroutine</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isroutine" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a user-defined or built-in function or method.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.isabstract"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isabstract</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isabstract" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is an abstract base class.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.ismethoddescriptor"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">ismethoddescriptor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.ismethoddescriptor" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a method descriptor, but not if <a class="reference internal" href="#inspect.ismethod" title="inspect.ismethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">ismethod()</span></code></a>, <a class="reference internal" href="#inspect.isclass" title="inspect.isclass"><code class="xref py py-func docutils literal notranslate"><span class="pre">isclass()</span></code></a>, <a class="reference internal" href="#inspect.isfunction" title="inspect.isfunction"><code class="xref py py-func docutils literal notranslate"><span class="pre">isfunction()</span></code></a> or <a class="reference internal" href="#inspect.isbuiltin" title="inspect.isbuiltin"><code class="xref py py-func docutils literal notranslate"><span class="pre">isbuiltin()</span></code></a> are true.</p> <p>This, for example, is true of <code class="docutils literal notranslate"><span class="pre">int.__add__</span></code>. An object passing this test has a <a class="reference internal" href="../reference/datamodel.html#object.__get__" title="object.__get__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__get__()</span></code></a> method but not a <a class="reference internal" href="../reference/datamodel.html#object.__set__" title="object.__set__"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__set__()</span></code></a> method, but beyond that the set of attributes varies. A <a class="reference internal" href="stdtypes.html#definition.__name__" title="definition.__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a> attribute is usually sensible, and <code class="xref py py-attr docutils literal notranslate"><span class="pre">__doc__</span></code> often is.</p> <p>Methods implemented via descriptors that also pass one of the other tests return <code class="docutils literal notranslate"><span class="pre">False</span></code> from the <a class="reference internal" href="#inspect.ismethoddescriptor" title="inspect.ismethoddescriptor"><code class="xref py py-func docutils literal notranslate"><span class="pre">ismethoddescriptor()</span></code></a> test, simply because the other tests promise more – you can, e.g., count on having the <code class="xref py py-attr docutils literal notranslate"><span class="pre">__func__</span></code> attribute (etc) when an object passes <a class="reference internal" href="#inspect.ismethod" title="inspect.ismethod"><code class="xref py py-func docutils literal notranslate"><span class="pre">ismethod()</span></code></a>.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.isdatadescriptor"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isdatadescriptor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isdatadescriptor" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a data descriptor.</p> <p>Data descriptors have a <a class="reference internal" href="../reference/datamodel.html#object.__set__" title="object.__set__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__set__</span></code></a> or a <a class="reference internal" href="../reference/datamodel.html#object.__delete__" title="object.__delete__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__delete__</span></code></a> method. Examples are properties (defined in Python), getsets, and members. The latter two are defined in C and there are more specific tests available for those types, which is robust across Python implementations. Typically, data descriptors will also have <a class="reference internal" href="stdtypes.html#definition.__name__" title="definition.__name__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__name__</span></code></a> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">__doc__</span></code> attributes (properties, getsets, and members have both of these attributes), but this is not guaranteed.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.isgetsetdescriptor"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">isgetsetdescriptor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.isgetsetdescriptor" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a getset descriptor.</p> <div class="impl-detail compound"> <p><strong>CPython implementation detail:</strong> getsets are attributes defined in extension modules via <a class="reference internal" href="../c-api/structures.html#c.PyGetSetDef" title="PyGetSetDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyGetSetDef</span></code></a> structures. For Python implementations without such types, this method will always return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.ismemberdescriptor"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">ismemberdescriptor</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.ismemberdescriptor" title="Permalink to this definition">¶</a></dt> <dd><p>Return <code class="docutils literal notranslate"><span class="pre">True</span></code> if the object is a member descriptor.</p> <div class="impl-detail compound"> <p><strong>CPython implementation detail:</strong> Member descriptors are attributes defined in extension modules via <a class="reference internal" href="../c-api/structures.html#c.PyMemberDef" title="PyMemberDef"><code class="xref c c-type docutils literal notranslate"><span class="pre">PyMemberDef</span></code></a> structures. For Python implementations without such types, this method will always return <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p> </div> </dd></dl> </section> <section id="retrieving-source-code"> <span id="inspect-source"></span><h2>Retrieving source code<a class="headerlink" href="#retrieving-source-code" title="Permalink to this headline">¶</a></h2> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getdoc"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getdoc</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getdoc" title="Permalink to this definition">¶</a></dt> <dd><p>Get the documentation string for an object, cleaned up with <a class="reference internal" href="#inspect.cleandoc" title="inspect.cleandoc"><code class="xref py py-func docutils literal notranslate"><span class="pre">cleandoc()</span></code></a>. If the documentation string for an object is not provided and the object is a class, a method, a property or a descriptor, retrieve the documentation string from the inheritance hierarchy. Return <code class="docutils literal notranslate"><span class="pre">None</span></code> if the documentation string is invalid or missing.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>Documentation strings are now inherited if not overridden.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getcomments"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getcomments</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getcomments" title="Permalink to this definition">¶</a></dt> <dd><p>Return in a single string any lines of comments immediately preceding the object’s source code (for a class, function, or method), or at the top of the Python source file (if the object is a module). If the object’s source code is unavailable, return <code class="docutils literal notranslate"><span class="pre">None</span></code>. This could happen if the object has been defined in C or the interactive shell.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getfile"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getfile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getfile" title="Permalink to this definition">¶</a></dt> <dd><p>Return the name of the (text or binary) file in which an object was defined. This will fail with a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if the object is a built-in module, class, or function.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getmodule"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getmodule</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getmodule" title="Permalink to this definition">¶</a></dt> <dd><p>Try to guess which module an object was defined in. Return <code class="docutils literal notranslate"><span class="pre">None</span></code> if the module cannot be determined.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getsourcefile"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getsourcefile</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getsourcefile" title="Permalink to this definition">¶</a></dt> <dd><p>Return the name of the Python source file in which an object was defined or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no way can be identified to get the source. This will fail with a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if the object is a built-in module, class, or function.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getsourcelines"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getsourcelines</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getsourcelines" title="Permalink to this definition">¶</a></dt> <dd><p>Return a list of source lines and starting line number for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a list of the lines corresponding to the object and the line number indicates where in the original source file the first line of code was found. An <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised if the source code cannot be retrieved. A <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised if the object is a built-in module, class, or function.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span><a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised instead of <a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a>, now an alias of the former.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getsource"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getsource</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">object</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getsource" title="Permalink to this definition">¶</a></dt> <dd><p>Return the text of the source code for an object. The argument may be a module, class, method, function, traceback, frame, or code object. The source code is returned as a single string. An <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised if the source code cannot be retrieved. A <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised if the object is a built-in module, class, or function.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.3: </span><a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised instead of <a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a>, now an alias of the former.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.cleandoc"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">cleandoc</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">doc</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.cleandoc" title="Permalink to this definition">¶</a></dt> <dd><p>Clean up indentation from docstrings that are indented to line up with blocks of code.</p> <p>All leading whitespace is removed from the first line. Any leading whitespace that can be uniformly removed from the second line onwards is removed. Empty lines at the beginning and end are subsequently removed. Also, all tabs are expanded to spaces.</p> </dd></dl> </section> <section id="introspecting-callables-with-the-signature-object"> <span id="inspect-signature-object"></span><h2>Introspecting callables with the Signature object<a class="headerlink" href="#introspecting-callables-with-the-signature-object" title="Permalink to this headline">¶</a></h2> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> <p>The Signature object represents the call signature of a callable object and its return annotation. To retrieve a Signature object, use the <a class="reference internal" href="#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a> function.</p> <dl class="py function"> <dt class="sig sig-object py" id="inspect.signature"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">signature</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">callable</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_wrapped</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">locals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">eval_str</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.signature" title="Permalink to this definition">¶</a></dt> <dd><p>Return a <a class="reference internal" href="#inspect.Signature" title="inspect.Signature"><code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code></a> object for the given <code class="docutils literal notranslate"><span class="pre">callable</span></code>:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">inspect</span> <span class="kn">import</span> <span class="n">signature</span> <span class="gp">>>> </span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span><span class="nb">int</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> <span class="gp">... </span> <span class="k">pass</span> <span class="gp">>>> </span><span class="n">sig</span> <span class="o">=</span> <span class="n">signature</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span> <span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">sig</span><span class="p">)</span> <span class="go">'(a, *, b:int, **kwargs)'</span> <span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">sig</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s1">'b'</span><span class="p">])</span> <span class="go">'b:int'</span> <span class="gp">>>> </span><span class="n">sig</span><span class="o">.</span><span class="n">parameters</span><span class="p">[</span><span class="s1">'b'</span><span class="p">]</span><span class="o">.</span><span class="n">annotation</span> <span class="go"><class 'int'></span> </pre></div> </div> <p>Accepts a wide range of Python callables, from plain functions and classes to <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> objects.</p> <p>For objects defined in modules using stringized annotations (<code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">annotations</span></code>), <a class="reference internal" href="#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a> will attempt to automatically un-stringize the annotations using <a class="reference internal" href="#inspect.get_annotations" title="inspect.get_annotations"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.get_annotations()</span></code></a>. The <code class="docutils literal notranslate"><span class="pre">global</span></code>, <code class="docutils literal notranslate"><span class="pre">locals</span></code>, and <code class="docutils literal notranslate"><span class="pre">eval_str</span></code> parameters are passed into <a class="reference internal" href="#inspect.get_annotations" title="inspect.get_annotations"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.get_annotations()</span></code></a> when resolving the annotations; see the documentation for <a class="reference internal" href="#inspect.get_annotations" title="inspect.get_annotations"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.get_annotations()</span></code></a> for instructions on how to use these parameters.</p> <p>Raises <a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> if no signature can be provided, and <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if that type of object is not supported. Also, if the annotations are stringized, and <code class="docutils literal notranslate"><span class="pre">eval_str</span></code> is not false, the <code class="docutils literal notranslate"><span class="pre">eval()</span></code> call(s) to un-stringize the annotations could potentially raise any kind of exception.</p> <p>A slash(/) in the signature of a function denotes that the parameters prior to it are positional-only. For more info, see <a class="reference internal" href="../faq/programming.html#faq-positional-only-arguments"><span class="std std-ref">the FAQ entry on positional-only parameters</span></a>.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5: </span><code class="docutils literal notranslate"><span class="pre">follow_wrapped</span></code> parameter. Pass <code class="docutils literal notranslate"><span class="pre">False</span></code> to get a signature of <code class="docutils literal notranslate"><span class="pre">callable</span></code> specifically (<code class="docutils literal notranslate"><span class="pre">callable.__wrapped__</span></code> will not be used to unwrap decorated callables.)</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10: </span><code class="docutils literal notranslate"><span class="pre">globals</span></code>, <code class="docutils literal notranslate"><span class="pre">locals</span></code>, and <code class="docutils literal notranslate"><span class="pre">eval_str</span></code> parameters.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Some callables may not be introspectable in certain implementations of Python. For example, in CPython, some built-in functions defined in C provide no metadata about their arguments.</p> </div> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="inspect.Signature"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">Signature</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">parameters</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">return_annotation</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">Signature.empty</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.Signature" title="Permalink to this definition">¶</a></dt> <dd><p>A Signature object represents the call signature of a function and its return annotation. For each parameter accepted by the function it stores a <a class="reference internal" href="#inspect.Parameter" title="inspect.Parameter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code></a> object in its <a class="reference internal" href="#inspect.Signature.parameters" title="inspect.Signature.parameters"><code class="xref py py-attr docutils literal notranslate"><span class="pre">parameters</span></code></a> collection.</p> <p>The optional <em>parameters</em> argument is a sequence of <a class="reference internal" href="#inspect.Parameter" title="inspect.Parameter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code></a> objects, which is validated to check that there are no parameters with duplicate names, and that the parameters are in the right order, i.e. positional-only first, then positional-or-keyword, and that parameters with defaults follow parameters without defaults.</p> <p>The optional <em>return_annotation</em> argument, can be an arbitrary Python object, is the “return” annotation of the callable.</p> <p>Signature objects are <em>immutable</em>. Use <a class="reference internal" href="#inspect.Signature.replace" title="inspect.Signature.replace"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.replace()</span></code></a> to make a modified copy.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>Signature objects are picklable and <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a>.</p> </div> <dl class="py attribute"> <dt class="sig sig-object py" id="inspect.Signature.empty"> <span class="sig-name descname"><span class="pre">empty</span></span><a class="headerlink" href="#inspect.Signature.empty" title="Permalink to this definition">¶</a></dt> <dd><p>A special class-level marker to specify absence of a return annotation.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="inspect.Signature.parameters"> <span class="sig-name descname"><span class="pre">parameters</span></span><a class="headerlink" href="#inspect.Signature.parameters" title="Permalink to this definition">¶</a></dt> <dd><p>An ordered mapping of parameters’ names to the corresponding <a class="reference internal" href="#inspect.Parameter" title="inspect.Parameter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code></a> objects. Parameters appear in strict definition order, including keyword-only parameters.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>Python only explicitly guaranteed that it preserved the declaration order of keyword-only parameters as of version 3.7, although in practice this order had always been preserved in Python 3.</p> </div> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="inspect.Signature.return_annotation"> <span class="sig-name descname"><span class="pre">return_annotation</span></span><a class="headerlink" href="#inspect.Signature.return_annotation" title="Permalink to this definition">¶</a></dt> <dd><p>The “return” annotation for the callable. If the callable has no “return” annotation, this attribute is set to <a class="reference internal" href="#inspect.Signature.empty" title="inspect.Signature.empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Signature.empty</span></code></a>.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="inspect.Signature.bind"> <span class="sig-name descname"><span class="pre">bind</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.Signature.bind" title="Permalink to this definition">¶</a></dt> <dd><p>Create a mapping from positional and keyword arguments to parameters. Returns <a class="reference internal" href="#inspect.BoundArguments" title="inspect.BoundArguments"><code class="xref py py-class docutils literal notranslate"><span class="pre">BoundArguments</span></code></a> if <code class="docutils literal notranslate"><span class="pre">*args</span></code> and <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code> match the signature, or raises a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="inspect.Signature.bind_partial"> <span class="sig-name descname"><span class="pre">bind_partial</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwargs</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.Signature.bind_partial" title="Permalink to this definition">¶</a></dt> <dd><p>Works the same way as <a class="reference internal" href="#inspect.Signature.bind" title="inspect.Signature.bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.bind()</span></code></a>, but allows the omission of some required arguments (mimics <a class="reference internal" href="functools.html#functools.partial" title="functools.partial"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.partial()</span></code></a> behavior.) Returns <a class="reference internal" href="#inspect.BoundArguments" title="inspect.BoundArguments"><code class="xref py py-class docutils literal notranslate"><span class="pre">BoundArguments</span></code></a>, or raises a <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> if the passed arguments do not match the signature.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="inspect.Signature.replace"> <span class="sig-name descname"><span class="pre">replace</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">*[,</span> <span class="pre">parameters][,</span> <span class="pre">return_annotation]</span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.Signature.replace" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new Signature instance based on the instance replace was invoked on. It is possible to pass different <code class="docutils literal notranslate"><span class="pre">parameters</span></code> and/or <code class="docutils literal notranslate"><span class="pre">return_annotation</span></code> to override the corresponding properties of the base signature. To remove return_annotation from the copied Signature, pass in <a class="reference internal" href="#inspect.Signature.empty" title="inspect.Signature.empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Signature.empty</span></code></a>.</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> <span class="gp">... </span> <span class="k">pass</span> <span class="gp">>>> </span><span class="n">sig</span> <span class="o">=</span> <span class="n">signature</span><span class="p">(</span><span class="n">test</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">new_sig</span> <span class="o">=</span> <span class="n">sig</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">return_annotation</span><span class="o">=</span><span class="s2">"new return anno"</span><span class="p">)</span> <span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">new_sig</span><span class="p">)</span> <span class="go">"(a, b) -> 'new return anno'"</span> </pre></div> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="inspect.Signature.from_callable"> <em class="property"><span class="pre">classmethod</span><span class="w"> </span></em><span class="sig-name descname"><span class="pre">from_callable</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">follow_wrapped</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globalns</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">localns</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.Signature.from_callable" title="Permalink to this definition">¶</a></dt> <dd><p>Return a <a class="reference internal" href="#inspect.Signature" title="inspect.Signature"><code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code></a> (or its subclass) object for a given callable <code class="docutils literal notranslate"><span class="pre">obj</span></code>. Pass <code class="docutils literal notranslate"><span class="pre">follow_wrapped=False</span></code> to get a signature of <code class="docutils literal notranslate"><span class="pre">obj</span></code> without unwrapping its <code class="docutils literal notranslate"><span class="pre">__wrapped__</span></code> chain. <code class="docutils literal notranslate"><span class="pre">globalns</span></code> and <code class="docutils literal notranslate"><span class="pre">localns</span></code> will be used as the namespaces when resolving annotations.</p> <p>This method simplifies subclassing of <a class="reference internal" href="#inspect.Signature" title="inspect.Signature"><code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code></a>:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MySignature</span><span class="p">(</span><span class="n">Signature</span><span class="p">):</span> <span class="k">pass</span> <span class="n">sig</span> <span class="o">=</span> <span class="n">MySignature</span><span class="o">.</span><span class="n">from_callable</span><span class="p">(</span><span class="nb">min</span><span class="p">)</span> <span class="k">assert</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">sig</span><span class="p">,</span> <span class="n">MySignature</span><span class="p">)</span> </pre></div> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10: </span><code class="docutils literal notranslate"><span class="pre">globalns</span></code> and <code class="docutils literal notranslate"><span class="pre">localns</span></code> parameters.</p> </div> </dd></dl> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="inspect.Parameter"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">Parameter</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kind</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">Parameter.empty</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">annotation</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">Parameter.empty</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.Parameter" title="Permalink to this definition">¶</a></dt> <dd><p>Parameter objects are <em>immutable</em>. Instead of modifying a Parameter object, you can use <a class="reference internal" href="#inspect.Parameter.replace" title="inspect.Parameter.replace"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Parameter.replace()</span></code></a> to create a modified copy.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>Parameter objects are picklable and <a class="reference internal" href="../glossary.html#term-hashable"><span class="xref std std-term">hashable</span></a>.</p> </div> <dl class="py attribute"> <dt class="sig sig-object py" id="inspect.Parameter.empty"> <span class="sig-name descname"><span class="pre">empty</span></span><a class="headerlink" href="#inspect.Parameter.empty" title="Permalink to this definition">¶</a></dt> <dd><p>A special class-level marker to specify absence of default values and annotations.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="inspect.Parameter.name"> <span class="sig-name descname"><span class="pre">name</span></span><a class="headerlink" href="#inspect.Parameter.name" title="Permalink to this definition">¶</a></dt> <dd><p>The name of the parameter as a string. The name must be a valid Python identifier.</p> <div class="impl-detail compound"> <p class="compound-first"><strong>CPython implementation detail:</strong> CPython generates implicit parameter names of the form <code class="docutils literal notranslate"><span class="pre">.0</span></code> on the code objects used to implement comprehensions and generator expressions.</p> <div class="compound-last versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>These parameter names are exposed by this module as names like <code class="docutils literal notranslate"><span class="pre">implicit0</span></code>.</p> </div> </div> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="inspect.Parameter.default"> <span class="sig-name descname"><span class="pre">default</span></span><a class="headerlink" href="#inspect.Parameter.default" title="Permalink to this definition">¶</a></dt> <dd><p>The default value for the parameter. If the parameter has no default value, this attribute is set to <a class="reference internal" href="#inspect.Parameter.empty" title="inspect.Parameter.empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Parameter.empty</span></code></a>.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="inspect.Parameter.annotation"> <span class="sig-name descname"><span class="pre">annotation</span></span><a class="headerlink" href="#inspect.Parameter.annotation" title="Permalink to this definition">¶</a></dt> <dd><p>The annotation for the parameter. If the parameter has no annotation, this attribute is set to <a class="reference internal" href="#inspect.Parameter.empty" title="inspect.Parameter.empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Parameter.empty</span></code></a>.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="inspect.Parameter.kind"> <span class="sig-name descname"><span class="pre">kind</span></span><a class="headerlink" href="#inspect.Parameter.kind" title="Permalink to this definition">¶</a></dt> <dd><p>Describes how argument values are bound to the parameter. The possible values are accessible via <a class="reference internal" href="#inspect.Parameter" title="inspect.Parameter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code></a> (like <code class="docutils literal notranslate"><span class="pre">Parameter.KEYWORD_ONLY</span></code>), and support comparison and ordering, in the following order:</p> <table class="docutils align-default"> <colgroup> <col style="width: 34%" /> <col style="width: 66%" /> </colgroup> <thead> <tr class="row-odd"><th class="head"><p>Name</p></th> <th class="head"><p>Meaning</p></th> </tr> </thead> <tbody> <tr class="row-even"><td><p><em>POSITIONAL_ONLY</em></p></td> <td><p>Value must be supplied as a positional argument. Positional only parameters are those which appear before a <code class="docutils literal notranslate"><span class="pre">/</span></code> entry (if present) in a Python function definition.</p></td> </tr> <tr class="row-odd"><td><p><em>POSITIONAL_OR_KEYWORD</em></p></td> <td><p>Value may be supplied as either a keyword or positional argument (this is the standard binding behaviour for functions implemented in Python.)</p></td> </tr> <tr class="row-even"><td><p><em>VAR_POSITIONAL</em></p></td> <td><p>A tuple of positional arguments that aren’t bound to any other parameter. This corresponds to a <code class="docutils literal notranslate"><span class="pre">*args</span></code> parameter in a Python function definition.</p></td> </tr> <tr class="row-odd"><td><p><em>KEYWORD_ONLY</em></p></td> <td><p>Value must be supplied as a keyword argument. Keyword only parameters are those which appear after a <code class="docutils literal notranslate"><span class="pre">*</span></code> or <code class="docutils literal notranslate"><span class="pre">*args</span></code> entry in a Python function definition.</p></td> </tr> <tr class="row-even"><td><p><em>VAR_KEYWORD</em></p></td> <td><p>A dict of keyword arguments that aren’t bound to any other parameter. This corresponds to a <code class="docutils literal notranslate"><span class="pre">**kwargs</span></code> parameter in a Python function definition.</p></td> </tr> </tbody> </table> <p>Example: print all keyword-only arguments without default values:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="mi">10</span><span class="p">):</span> <span class="gp">... </span> <span class="k">pass</span> <span class="gp">>>> </span><span class="n">sig</span> <span class="o">=</span> <span class="n">signature</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">sig</span><span class="o">.</span><span class="n">parameters</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> <span class="gp">... </span> <span class="k">if</span> <span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">kind</span> <span class="o">==</span> <span class="n">param</span><span class="o">.</span><span class="n">KEYWORD_ONLY</span> <span class="ow">and</span> <span class="gp">... </span> <span class="n">param</span><span class="o">.</span><span class="n">default</span> <span class="ow">is</span> <span class="n">param</span><span class="o">.</span><span class="n">empty</span><span class="p">):</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s1">'Parameter:'</span><span class="p">,</span> <span class="n">param</span><span class="p">)</span> <span class="go">Parameter: c</span> </pre></div> </div> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="inspect.Parameter.kind.description"> <span class="sig-prename descclassname"><span class="pre">kind.</span></span><span class="sig-name descname"><span class="pre">description</span></span><a class="headerlink" href="#inspect.Parameter.kind.description" title="Permalink to this definition">¶</a></dt> <dd><p>Describes a enum value of Parameter.kind.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.8.</span></p> </div> <p>Example: print all descriptions of arguments:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">d</span><span class="o">=</span><span class="mi">10</span><span class="p">):</span> <span class="gp">... </span> <span class="k">pass</span> <span class="gp">>>> </span><span class="n">sig</span> <span class="o">=</span> <span class="n">signature</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span> <span class="gp">>>> </span><span class="k">for</span> <span class="n">param</span> <span class="ow">in</span> <span class="n">sig</span><span class="o">.</span><span class="n">parameters</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> <span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">kind</span><span class="o">.</span><span class="n">description</span><span class="p">)</span> <span class="go">positional or keyword</span> <span class="go">positional or keyword</span> <span class="go">keyword-only</span> <span class="go">keyword-only</span> </pre></div> </div> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="inspect.Parameter.replace"> <span class="sig-name descname"><span class="pre">replace</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="pre">*[,</span> <span class="pre">name][,</span> <span class="pre">kind][,</span> <span class="pre">default][,</span> <span class="pre">annotation]</span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.Parameter.replace" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new Parameter instance based on the instance replaced was invoked on. To override a <a class="reference internal" href="#inspect.Parameter" title="inspect.Parameter"><code class="xref py py-class docutils literal notranslate"><span class="pre">Parameter</span></code></a> attribute, pass the corresponding argument. To remove a default value or/and an annotation from a Parameter, pass <a class="reference internal" href="#inspect.Parameter.empty" title="inspect.Parameter.empty"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Parameter.empty</span></code></a>.</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">inspect</span> <span class="kn">import</span> <span class="n">Parameter</span> <span class="gp">>>> </span><span class="n">param</span> <span class="o">=</span> <span class="n">Parameter</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">,</span> <span class="n">Parameter</span><span class="o">.</span><span class="n">KEYWORD_ONLY</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mi">42</span><span class="p">)</span> <span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">param</span><span class="p">)</span> <span class="go">'foo=42'</span> <span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">replace</span><span class="p">())</span> <span class="c1"># Will create a shallow copy of 'param'</span> <span class="go">'foo=42'</span> <span class="gp">>>> </span><span class="nb">str</span><span class="p">(</span><span class="n">param</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">default</span><span class="o">=</span><span class="n">Parameter</span><span class="o">.</span><span class="n">empty</span><span class="p">,</span> <span class="n">annotation</span><span class="o">=</span><span class="s1">'spam'</span><span class="p">))</span> <span class="go">"foo:'spam'"</span> </pre></div> </div> </dd></dl> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>In Python 3.3 Parameter objects were allowed to have <code class="docutils literal notranslate"><span class="pre">name</span></code> set to <code class="docutils literal notranslate"><span class="pre">None</span></code> if their <code class="docutils literal notranslate"><span class="pre">kind</span></code> was set to <code class="docutils literal notranslate"><span class="pre">POSITIONAL_ONLY</span></code>. This is no longer permitted.</p> </div> </dd></dl> <dl class="py class"> <dt class="sig sig-object py" id="inspect.BoundArguments"> <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">BoundArguments</span></span><a class="headerlink" href="#inspect.BoundArguments" title="Permalink to this definition">¶</a></dt> <dd><p>Result of a <a class="reference internal" href="#inspect.Signature.bind" title="inspect.Signature.bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.bind()</span></code></a> or <a class="reference internal" href="#inspect.Signature.bind_partial" title="inspect.Signature.bind_partial"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.bind_partial()</span></code></a> call. Holds the mapping of arguments to the function’s parameters.</p> <dl class="py attribute"> <dt class="sig sig-object py" id="inspect.BoundArguments.arguments"> <span class="sig-name descname"><span class="pre">arguments</span></span><a class="headerlink" href="#inspect.BoundArguments.arguments" title="Permalink to this definition">¶</a></dt> <dd><p>A mutable mapping of parameters’ names to arguments’ values. Contains only explicitly bound arguments. Changes in <a class="reference internal" href="#inspect.BoundArguments.arguments" title="inspect.BoundArguments.arguments"><code class="xref py py-attr docutils literal notranslate"><span class="pre">arguments</span></code></a> will reflect in <a class="reference internal" href="#inspect.BoundArguments.args" title="inspect.BoundArguments.args"><code class="xref py py-attr docutils literal notranslate"><span class="pre">args</span></code></a> and <a class="reference internal" href="#inspect.BoundArguments.kwargs" title="inspect.BoundArguments.kwargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">kwargs</span></code></a>.</p> <p>Should be used in conjunction with <a class="reference internal" href="#inspect.Signature.parameters" title="inspect.Signature.parameters"><code class="xref py py-attr docutils literal notranslate"><span class="pre">Signature.parameters</span></code></a> for any argument processing purposes.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Arguments for which <a class="reference internal" href="#inspect.Signature.bind" title="inspect.Signature.bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.bind()</span></code></a> or <a class="reference internal" href="#inspect.Signature.bind_partial" title="inspect.Signature.bind_partial"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.bind_partial()</span></code></a> relied on a default value are skipped. However, if needed, use <a class="reference internal" href="#inspect.BoundArguments.apply_defaults" title="inspect.BoundArguments.apply_defaults"><code class="xref py py-meth docutils literal notranslate"><span class="pre">BoundArguments.apply_defaults()</span></code></a> to add them.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.9: </span><a class="reference internal" href="#inspect.BoundArguments.arguments" title="inspect.BoundArguments.arguments"><code class="xref py py-attr docutils literal notranslate"><span class="pre">arguments</span></code></a> is now of type <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a>. Formerly, it was of type <a class="reference internal" href="collections.html#collections.OrderedDict" title="collections.OrderedDict"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.OrderedDict</span></code></a>.</p> </div> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="inspect.BoundArguments.args"> <span class="sig-name descname"><span class="pre">args</span></span><a class="headerlink" href="#inspect.BoundArguments.args" title="Permalink to this definition">¶</a></dt> <dd><p>A tuple of positional arguments values. Dynamically computed from the <a class="reference internal" href="#inspect.BoundArguments.arguments" title="inspect.BoundArguments.arguments"><code class="xref py py-attr docutils literal notranslate"><span class="pre">arguments</span></code></a> attribute.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="inspect.BoundArguments.kwargs"> <span class="sig-name descname"><span class="pre">kwargs</span></span><a class="headerlink" href="#inspect.BoundArguments.kwargs" title="Permalink to this definition">¶</a></dt> <dd><p>A dict of keyword arguments values. Dynamically computed from the <a class="reference internal" href="#inspect.BoundArguments.arguments" title="inspect.BoundArguments.arguments"><code class="xref py py-attr docutils literal notranslate"><span class="pre">arguments</span></code></a> attribute.</p> </dd></dl> <dl class="py attribute"> <dt class="sig sig-object py" id="inspect.BoundArguments.signature"> <span class="sig-name descname"><span class="pre">signature</span></span><a class="headerlink" href="#inspect.BoundArguments.signature" title="Permalink to this definition">¶</a></dt> <dd><p>A reference to the parent <a class="reference internal" href="#inspect.Signature" title="inspect.Signature"><code class="xref py py-class docutils literal notranslate"><span class="pre">Signature</span></code></a> object.</p> </dd></dl> <dl class="py method"> <dt class="sig sig-object py" id="inspect.BoundArguments.apply_defaults"> <span class="sig-name descname"><span class="pre">apply_defaults</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#inspect.BoundArguments.apply_defaults" title="Permalink to this definition">¶</a></dt> <dd><p>Set default values for missing arguments.</p> <p>For variable-positional arguments (<code class="docutils literal notranslate"><span class="pre">*args</span></code>) the default is an empty tuple.</p> <p>For variable-keyword arguments (<code class="docutils literal notranslate"><span class="pre">**kwargs</span></code>) the default is an empty dict.</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="s1">'ham'</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span> <span class="k">pass</span> <span class="gp">>>> </span><span class="n">ba</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">signature</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span><span class="o">.</span><span class="n">bind</span><span class="p">(</span><span class="s1">'spam'</span><span class="p">)</span> <span class="gp">>>> </span><span class="n">ba</span><span class="o">.</span><span class="n">apply_defaults</span><span class="p">()</span> <span class="gp">>>> </span><span class="n">ba</span><span class="o">.</span><span class="n">arguments</span> <span class="go">{'a': 'spam', 'b': 'ham', 'args': ()}</span> </pre></div> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd></dl> <p>The <a class="reference internal" href="#inspect.BoundArguments.args" title="inspect.BoundArguments.args"><code class="xref py py-attr docutils literal notranslate"><span class="pre">args</span></code></a> and <a class="reference internal" href="#inspect.BoundArguments.kwargs" title="inspect.BoundArguments.kwargs"><code class="xref py py-attr docutils literal notranslate"><span class="pre">kwargs</span></code></a> properties can be used to invoke functions:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">b</span><span class="p">):</span> <span class="o">...</span> <span class="n">sig</span> <span class="o">=</span> <span class="n">signature</span><span class="p">(</span><span class="n">test</span><span class="p">)</span> <span class="n">ba</span> <span class="o">=</span> <span class="n">sig</span><span class="o">.</span><span class="n">bind</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">20</span><span class="p">)</span> <span class="n">test</span><span class="p">(</span><span class="o">*</span><span class="n">ba</span><span class="o">.</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">ba</span><span class="o">.</span><span class="n">kwargs</span><span class="p">)</span> </pre></div> </div> </dd></dl> <div class="admonition seealso"> <p class="admonition-title">See also</p> <dl class="simple"> <dt><span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0362"><strong>PEP 362</strong></a> - Function Signature Object.</dt><dd><p>The detailed specification, implementation details and examples.</p> </dd> </dl> </div> </section> <section id="classes-and-functions"> <span id="inspect-classes-functions"></span><h2>Classes and functions<a class="headerlink" href="#classes-and-functions" title="Permalink to this headline">¶</a></h2> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getclasstree"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getclasstree</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">classes</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">unique</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getclasstree" title="Permalink to this definition">¶</a></dt> <dd><p>Arrange the given list of classes into a hierarchy of nested lists. Where a nested list appears, it contains classes derived from the class whose entry immediately precedes the list. Each entry is a 2-tuple containing a class and a tuple of its base classes. If the <em>unique</em> argument is true, exactly one entry appears in the returned structure for each class in the given list. Otherwise, classes using multiple inheritance and their descendants will appear multiple times.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getargspec"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getargspec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getargspec" title="Permalink to this definition">¶</a></dt> <dd><p>Get the names and default values of a Python function’s parameters. A <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> <code class="docutils literal notranslate"><span class="pre">ArgSpec(args,</span> <span class="pre">varargs,</span> <span class="pre">keywords,</span> <span class="pre">defaults)</span></code> is returned. <em>args</em> is a list of the parameter names. <em>varargs</em> and <em>keywords</em> are the names of the <code class="docutils literal notranslate"><span class="pre">*</span></code> and <code class="docutils literal notranslate"><span class="pre">**</span></code> parameters or <code class="docutils literal notranslate"><span class="pre">None</span></code>. <em>defaults</em> is a tuple of default argument values or <code class="docutils literal notranslate"><span class="pre">None</span></code> if there are no default arguments; if this tuple has <em>n</em> elements, they correspond to the last <em>n</em> elements listed in <em>args</em>.</p> <div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 3.0: </span>Use <a class="reference internal" href="#inspect.getfullargspec" title="inspect.getfullargspec"><code class="xref py py-func docutils literal notranslate"><span class="pre">getfullargspec()</span></code></a> for an updated API that is usually a drop-in replacement, but also correctly handles function annotations and keyword-only parameters.</p> <p>Alternatively, use <a class="reference internal" href="#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a> and <a class="reference internal" href="#inspect-signature-object"><span class="std std-ref">Signature Object</span></a>, which provide a more structured introspection API for callables.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getfullargspec"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getfullargspec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getfullargspec" title="Permalink to this definition">¶</a></dt> <dd><p>Get the names and default values of a Python function’s parameters. A <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> is returned:</p> <p><code class="docutils literal notranslate"><span class="pre">FullArgSpec(args,</span> <span class="pre">varargs,</span> <span class="pre">varkw,</span> <span class="pre">defaults,</span> <span class="pre">kwonlyargs,</span> <span class="pre">kwonlydefaults,</span> <span class="pre">annotations)</span></code></p> <p><em>args</em> is a list of the positional parameter names. <em>varargs</em> is the name of the <code class="docutils literal notranslate"><span class="pre">*</span></code> parameter or <code class="docutils literal notranslate"><span class="pre">None</span></code> if arbitrary positional arguments are not accepted. <em>varkw</em> is the name of the <code class="docutils literal notranslate"><span class="pre">**</span></code> parameter or <code class="docutils literal notranslate"><span class="pre">None</span></code> if arbitrary keyword arguments are not accepted. <em>defaults</em> is an <em>n</em>-tuple of default argument values corresponding to the last <em>n</em> positional parameters, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if there are no such defaults defined. <em>kwonlyargs</em> is a list of keyword-only parameter names in declaration order. <em>kwonlydefaults</em> is a dictionary mapping parameter names from <em>kwonlyargs</em> to the default values used if no argument is supplied. <em>annotations</em> is a dictionary mapping parameter names to annotations. The special key <code class="docutils literal notranslate"><span class="pre">"return"</span></code> is used to report the function return value annotation (if any).</p> <p>Note that <a class="reference internal" href="#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a> and <a class="reference internal" href="#inspect-signature-object"><span class="std std-ref">Signature Object</span></a> provide the recommended API for callable introspection, and support additional behaviours (like positional-only arguments) that are sometimes encountered in extension module APIs. This function is retained primarily for use in code that needs to maintain compatibility with the Python 2 <code class="docutils literal notranslate"><span class="pre">inspect</span></code> module API.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.4: </span>This function is now based on <a class="reference internal" href="#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a>, but still ignores <code class="docutils literal notranslate"><span class="pre">__wrapped__</span></code> attributes and includes the already bound first parameter in the signature output for bound methods.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.6: </span>This method was previously documented as deprecated in favour of <a class="reference internal" href="#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a> in Python 3.5, but that decision has been reversed in order to restore a clearly supported standard interface for single-source Python 2/3 code migrating away from the legacy <a class="reference internal" href="#inspect.getargspec" title="inspect.getargspec"><code class="xref py py-func docutils literal notranslate"><span class="pre">getargspec()</span></code></a> API.</p> </div> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.7: </span>Python only explicitly guaranteed that it preserved the declaration order of keyword-only parameters as of version 3.7, although in practice this order had always been preserved in Python 3.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getargvalues"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getargvalues</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">frame</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getargvalues" title="Permalink to this definition">¶</a></dt> <dd><p>Get information about arguments passed into a particular frame. A <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> <code class="docutils literal notranslate"><span class="pre">ArgInfo(args,</span> <span class="pre">varargs,</span> <span class="pre">keywords,</span> <span class="pre">locals)</span></code> is returned. <em>args</em> is a list of the argument names. <em>varargs</em> and <em>keywords</em> are the names of the <code class="docutils literal notranslate"><span class="pre">*</span></code> and <code class="docutils literal notranslate"><span class="pre">**</span></code> arguments or <code class="docutils literal notranslate"><span class="pre">None</span></code>. <em>locals</em> is the locals dictionary of the given frame.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This function was inadvertently marked as deprecated in Python 3.5.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.formatargspec"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">formatargspec</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">varargs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">varkw</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">defaults</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwonlyargs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">kwonlydefaults</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">annotations</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">formatarg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">formatvarargs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">formatvarkw</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">formatvalue</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">formatreturns</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">formatannotations</span></span></em><span class="optional">]</span><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#inspect.formatargspec" title="Permalink to this definition">¶</a></dt> <dd><p>Format a pretty argument spec from the values returned by <a class="reference internal" href="#inspect.getfullargspec" title="inspect.getfullargspec"><code class="xref py py-func docutils literal notranslate"><span class="pre">getfullargspec()</span></code></a>.</p> <p>The first seven arguments are (<code class="docutils literal notranslate"><span class="pre">args</span></code>, <code class="docutils literal notranslate"><span class="pre">varargs</span></code>, <code class="docutils literal notranslate"><span class="pre">varkw</span></code>, <code class="docutils literal notranslate"><span class="pre">defaults</span></code>, <code class="docutils literal notranslate"><span class="pre">kwonlyargs</span></code>, <code class="docutils literal notranslate"><span class="pre">kwonlydefaults</span></code>, <code class="docutils literal notranslate"><span class="pre">annotations</span></code>).</p> <p>The other six arguments are functions that are called to turn argument names, <code class="docutils literal notranslate"><span class="pre">*</span></code> argument name, <code class="docutils literal notranslate"><span class="pre">**</span></code> argument name, default values, return annotation and individual annotations into strings, respectively.</p> <p>For example:</p> <div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">inspect</span> <span class="kn">import</span> <span class="n">formatargspec</span><span class="p">,</span> <span class="n">getfullargspec</span> <span class="gp">>>> </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span> <span class="gp">... </span> <span class="k">pass</span> <span class="gp">...</span> <span class="gp">>>> </span><span class="n">formatargspec</span><span class="p">(</span><span class="o">*</span><span class="n">getfullargspec</span><span class="p">(</span><span class="n">f</span><span class="p">))</span> <span class="go">'(a: int, b: float)'</span> </pre></div> </div> <div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 3.5: </span>Use <a class="reference internal" href="#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a> and <a class="reference internal" href="#inspect-signature-object"><span class="std std-ref">Signature Object</span></a>, which provide a better introspecting API for callables.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.formatargvalues"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">formatargvalues</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">args</span></span></em><span class="optional">[</span>, <em class="sig-param"><span class="n"><span class="pre">varargs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">varkw</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">locals</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">formatarg</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">formatvarargs</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">formatvarkw</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">formatvalue</span></span></em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#inspect.formatargvalues" title="Permalink to this definition">¶</a></dt> <dd><p>Format a pretty argument spec from the four values returned by <a class="reference internal" href="#inspect.getargvalues" title="inspect.getargvalues"><code class="xref py py-func docutils literal notranslate"><span class="pre">getargvalues()</span></code></a>. The format* arguments are the corresponding optional formatting functions that are called to turn names and values into strings.</p> <div class="admonition note"> <p class="admonition-title">Note</p> <p>This function was inadvertently marked as deprecated in Python 3.5.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getmro"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getmro</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cls</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getmro" title="Permalink to this definition">¶</a></dt> <dd><p>Return a tuple of class cls’s base classes, including cls, in method resolution order. No class appears more than once in this tuple. Note that the method resolution order depends on cls’s type. Unless a very peculiar user-defined metatype is in use, cls will be the first element of the tuple.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getcallargs"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getcallargs</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">/</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span><span class="n"><span class="pre">args</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">**</span></span><span class="n"><span class="pre">kwds</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getcallargs" title="Permalink to this definition">¶</a></dt> <dd><p>Bind the <em>args</em> and <em>kwds</em> to the argument names of the Python function or method <em>func</em>, as if it was called with them. For bound methods, bind also the first argument (typically named <code class="docutils literal notranslate"><span class="pre">self</span></code>) to the associated instance. A dict is returned, mapping the argument names (including the names of the <code class="docutils literal notranslate"><span class="pre">*</span></code> and <code class="docutils literal notranslate"><span class="pre">**</span></code> arguments, if any) to their values from <em>args</em> and <em>kwds</em>. In case of invoking <em>func</em> incorrectly, i.e. whenever <code class="docutils literal notranslate"><span class="pre">func(*args,</span> <span class="pre">**kwds)</span></code> would raise an exception because of incompatible signature, an exception of the same type and the same or similar message is raised. For example:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">inspect</span> <span class="kn">import</span> <span class="n">getcallargs</span> <span class="gp">>>> </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="o">*</span><span class="n">pos</span><span class="p">,</span> <span class="o">**</span><span class="n">named</span><span class="p">):</span> <span class="gp">... </span> <span class="k">pass</span> <span class="gp">>>> </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="o">==</span> <span class="p">{</span><span class="s1">'a'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">'named'</span><span class="p">:</span> <span class="p">{},</span> <span class="s1">'b'</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">'pos'</span><span class="p">:</span> <span class="p">(</span><span class="mi">3</span><span class="p">,)}</span> <span class="go">True</span> <span class="gp">>>> </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">,</span> <span class="n">a</span><span class="o">=</span><span class="mi">2</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">4</span><span class="p">)</span> <span class="o">==</span> <span class="p">{</span><span class="s1">'a'</span><span class="p">:</span> <span class="mi">2</span><span class="p">,</span> <span class="s1">'named'</span><span class="p">:</span> <span class="p">{</span><span class="s1">'x'</span><span class="p">:</span> <span class="mi">4</span><span class="p">},</span> <span class="s1">'b'</span><span class="p">:</span> <span class="mi">1</span><span class="p">,</span> <span class="s1">'pos'</span><span class="p">:</span> <span class="p">()}</span> <span class="go">True</span> <span class="gp">>>> </span><span class="n">getcallargs</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="gt">Traceback (most recent call last):</span> <span class="c">...</span> <span class="gr">TypeError</span>: <span class="n">f() missing 1 required positional argument: 'a'</span> </pre></div> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> <div class="deprecated"> <p><span class="versionmodified deprecated">Deprecated since version 3.5: </span>Use <a class="reference internal" href="#inspect.Signature.bind" title="inspect.Signature.bind"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.bind()</span></code></a> and <a class="reference internal" href="#inspect.Signature.bind_partial" title="inspect.Signature.bind_partial"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Signature.bind_partial()</span></code></a> instead.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getclosurevars"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getclosurevars</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getclosurevars" title="Permalink to this definition">¶</a></dt> <dd><p>Get the mapping of external name references in a Python function or method <em>func</em> to their current values. A <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> <code class="docutils literal notranslate"><span class="pre">ClosureVars(nonlocals,</span> <span class="pre">globals,</span> <span class="pre">builtins,</span> <span class="pre">unbound)</span></code> is returned. <em>nonlocals</em> maps referenced names to lexical closure variables, <em>globals</em> to the function’s module globals and <em>builtins</em> to the builtins visible from the function body. <em>unbound</em> is the set of names referenced in the function that could not be resolved at all given the current module globals and builtins.</p> <p><a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised if <em>func</em> is not a Python function or method.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.unwrap"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">unwrap</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">stop</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.unwrap" title="Permalink to this definition">¶</a></dt> <dd><p>Get the object wrapped by <em>func</em>. It follows the chain of <code class="xref py py-attr docutils literal notranslate"><span class="pre">__wrapped__</span></code> attributes returning the last object in the chain.</p> <p><em>stop</em> is an optional callback accepting an object in the wrapper chain as its sole argument that allows the unwrapping to be terminated early if the callback returns a true value. If the callback never returns a true value, the last object in the chain is returned as usual. For example, <a class="reference internal" href="#inspect.signature" title="inspect.signature"><code class="xref py py-func docutils literal notranslate"><span class="pre">signature()</span></code></a> uses this to stop unwrapping if any object in the chain has a <code class="docutils literal notranslate"><span class="pre">__signature__</span></code> attribute defined.</p> <p><a class="reference internal" href="exceptions.html#ValueError" title="ValueError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ValueError</span></code></a> is raised if a cycle is encountered.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.4.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.get_annotations"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">get_annotations</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="o"><span class="pre">*</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">globals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">locals</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">eval_str</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.get_annotations" title="Permalink to this definition">¶</a></dt> <dd><p>Compute the annotations dict for an object.</p> <p><code class="docutils literal notranslate"><span class="pre">obj</span></code> may be a callable, class, or module. Passing in an object of any other type raises <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a>.</p> <p>Returns a dict. <code class="docutils literal notranslate"><span class="pre">get_annotations()</span></code> returns a new dict every time it’s called; calling it twice on the same object will return two different but equivalent dicts.</p> <p>This function handles several details for you:</p> <ul class="simple"> <li><p>If <code class="docutils literal notranslate"><span class="pre">eval_str</span></code> is true, values of type <code class="docutils literal notranslate"><span class="pre">str</span></code> will be un-stringized using <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>. This is intended for use with stringized annotations (<code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">__future__</span> <span class="pre">import</span> <span class="pre">annotations</span></code>).</p></li> <li><p>If <code class="docutils literal notranslate"><span class="pre">obj</span></code> doesn’t have an annotations dict, returns an empty dict. (Functions and methods always have an annotations dict; classes, modules, and other types of callables may not.)</p></li> <li><p>Ignores inherited annotations on classes. If a class doesn’t have its own annotations dict, returns an empty dict.</p></li> <li><p>All accesses to object members and dict values are done using <code class="docutils literal notranslate"><span class="pre">getattr()</span></code> and <code class="docutils literal notranslate"><span class="pre">dict.get()</span></code> for safety.</p></li> <li><p>Always, always, always returns a freshly created dict.</p></li> </ul> <p><code class="docutils literal notranslate"><span class="pre">eval_str</span></code> controls whether or not values of type <code class="docutils literal notranslate"><span class="pre">str</span></code> are replaced with the result of calling <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> on those values:</p> <ul class="simple"> <li><p>If eval_str is true, <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> is called on values of type <code class="docutils literal notranslate"><span class="pre">str</span></code>. (Note that <code class="docutils literal notranslate"><span class="pre">get_annotations</span></code> doesn’t catch exceptions; if <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> raises an exception, it will unwind the stack past the <code class="docutils literal notranslate"><span class="pre">get_annotations</span></code> call.)</p></li> <li><p>If eval_str is false (the default), values of type <code class="docutils literal notranslate"><span class="pre">str</span></code> are unchanged.</p></li> </ul> <p><code class="docutils literal notranslate"><span class="pre">globals</span></code> and <code class="docutils literal notranslate"><span class="pre">locals</span></code> are passed in to <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a>; see the documentation for <a class="reference internal" href="functions.html#eval" title="eval"><code class="xref py py-func docutils literal notranslate"><span class="pre">eval()</span></code></a> for more information. If <code class="docutils literal notranslate"><span class="pre">globals</span></code> or <code class="docutils literal notranslate"><span class="pre">locals</span></code> is <code class="docutils literal notranslate"><span class="pre">None</span></code>, this function may replace that value with a context-specific default, contingent on <code class="docutils literal notranslate"><span class="pre">type(obj)</span></code>:</p> <ul class="simple"> <li><p>If <code class="docutils literal notranslate"><span class="pre">obj</span></code> is a module, <code class="docutils literal notranslate"><span class="pre">globals</span></code> defaults to <code class="docutils literal notranslate"><span class="pre">obj.__dict__</span></code>.</p></li> <li><p>If <code class="docutils literal notranslate"><span class="pre">obj</span></code> is a class, <code class="docutils literal notranslate"><span class="pre">globals</span></code> defaults to <code class="docutils literal notranslate"><span class="pre">sys.modules[obj.__module__].__dict__</span></code> and <code class="docutils literal notranslate"><span class="pre">locals</span></code> defaults to the <code class="docutils literal notranslate"><span class="pre">obj</span></code> class namespace.</p></li> <li><p>If <code class="docutils literal notranslate"><span class="pre">obj</span></code> is a callable, <code class="docutils literal notranslate"><span class="pre">globals</span></code> defaults to <code class="docutils literal notranslate"><span class="pre">obj.__globals__</span></code>, although if <code class="docutils literal notranslate"><span class="pre">obj</span></code> is a wrapped function (using <code class="docutils literal notranslate"><span class="pre">functools.update_wrapper()</span></code>) it is first unwrapped.</p></li> </ul> <p>Calling <code class="docutils literal notranslate"><span class="pre">get_annotations</span></code> is best practice for accessing the annotations dict of any object. See <a class="reference internal" href="../howto/annotations.html#annotations-howto"><span class="std std-ref">Annotations Best Practices</span></a> for more information on annotations best practices.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.10.</span></p> </div> </dd></dl> </section> <section id="the-interpreter-stack"> <span id="inspect-stack"></span><h2>The interpreter stack<a class="headerlink" href="#the-interpreter-stack" title="Permalink to this headline">¶</a></h2> <p>When the following functions return “frame records,” each record is a <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> <code class="docutils literal notranslate"><span class="pre">FrameInfo(frame,</span> <span class="pre">filename,</span> <span class="pre">lineno,</span> <span class="pre">function,</span> <span class="pre">code_context,</span> <span class="pre">index)</span></code>. The tuple contains the frame object, the filename, the line number of the current line, the function name, a list of lines of context from the source code, and the index of the current line within that list.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>Return a named tuple instead of a tuple.</p> </div> <div class="admonition note"> <p class="admonition-title">Note</p> <p>Keeping references to frame objects, as found in the first element of the frame records these functions return, can cause your program to create reference cycles. Once a reference cycle has been created, the lifespan of all objects which can be accessed from the objects which form the cycle can become much longer even if Python’s optional cycle detector is enabled. If such cycles must be created, it is important to ensure they are explicitly broken to avoid the delayed destruction of objects and increased memory consumption which occurs.</p> <p>Though the cycle detector will catch these, destruction of the frames (and local variables) can be made deterministic by removing the cycle in a <a class="reference internal" href="../reference/compound_stmts.html#finally"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">finally</span></code></a> clause. This is also important if the cycle detector was disabled when Python was compiled or using <a class="reference internal" href="gc.html#gc.disable" title="gc.disable"><code class="xref py py-func docutils literal notranslate"><span class="pre">gc.disable()</span></code></a>. For example:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">handle_stackframe_without_leak</span><span class="p">():</span> <span class="n">frame</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">currentframe</span><span class="p">()</span> <span class="k">try</span><span class="p">:</span> <span class="c1"># do something with the frame</span> <span class="k">finally</span><span class="p">:</span> <span class="k">del</span> <span class="n">frame</span> </pre></div> </div> <p>If you want to keep the frame around (for example to print a traceback later), you can also break reference cycles by using the <a class="reference internal" href="../reference/datamodel.html#frame.clear" title="frame.clear"><code class="xref py py-meth docutils literal notranslate"><span class="pre">frame.clear()</span></code></a> method.</p> </div> <p>The optional <em>context</em> argument supported by most of these functions specifies the number of lines of context to return, which are centered around the current line.</p> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getframeinfo"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getframeinfo</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">frame</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getframeinfo" title="Permalink to this definition">¶</a></dt> <dd><p>Get information about a frame or traceback object. A <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuple</span></a> <code class="docutils literal notranslate"><span class="pre">Traceback(filename,</span> <span class="pre">lineno,</span> <span class="pre">function,</span> <span class="pre">code_context,</span> <span class="pre">index)</span></code> is returned.</p> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getouterframes"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getouterframes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">frame</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getouterframes" title="Permalink to this definition">¶</a></dt> <dd><p>Get a list of frame records for a frame and all outer frames. These frames represent the calls that lead to the creation of <em>frame</em>. The first entry in the returned list represents <em>frame</em>; the last entry represents the outermost call on <em>frame</em>’s stack.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>A list of <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuples</span></a> <code class="docutils literal notranslate"><span class="pre">FrameInfo(frame,</span> <span class="pre">filename,</span> <span class="pre">lineno,</span> <span class="pre">function,</span> <span class="pre">code_context,</span> <span class="pre">index)</span></code> is returned.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getinnerframes"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getinnerframes</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">traceback</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getinnerframes" title="Permalink to this definition">¶</a></dt> <dd><p>Get a list of frame records for a traceback’s frame and all inner frames. These frames represent calls made as a consequence of <em>frame</em>. The first entry in the list represents <em>traceback</em>; the last entry represents where the exception was raised.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>A list of <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuples</span></a> <code class="docutils literal notranslate"><span class="pre">FrameInfo(frame,</span> <span class="pre">filename,</span> <span class="pre">lineno,</span> <span class="pre">function,</span> <span class="pre">code_context,</span> <span class="pre">index)</span></code> is returned.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.currentframe"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">currentframe</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#inspect.currentframe" title="Permalink to this definition">¶</a></dt> <dd><p>Return the frame object for the caller’s stack frame.</p> <div class="impl-detail compound"> <p><strong>CPython implementation detail:</strong> This function relies on Python stack frame support in the interpreter, which isn’t guaranteed to exist in all implementations of Python. If running in an implementation without Python stack frame support this function returns <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.stack"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">stack</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.stack" title="Permalink to this definition">¶</a></dt> <dd><p>Return a list of frame records for the caller’s stack. The first entry in the returned list represents the caller; the last entry represents the outermost call on the stack.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>A list of <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuples</span></a> <code class="docutils literal notranslate"><span class="pre">FrameInfo(frame,</span> <span class="pre">filename,</span> <span class="pre">lineno,</span> <span class="pre">function,</span> <span class="pre">code_context,</span> <span class="pre">index)</span></code> is returned.</p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.trace"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">trace</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">context</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">1</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.trace" title="Permalink to this definition">¶</a></dt> <dd><p>Return a list of frame records for the stack between the current frame and the frame in which an exception currently being handled was raised in. The first entry in the list represents the caller; the last entry represents where the exception was raised.</p> <div class="versionchanged"> <p><span class="versionmodified changed">Changed in version 3.5: </span>A list of <a class="reference internal" href="../glossary.html#term-named-tuple"><span class="xref std std-term">named tuples</span></a> <code class="docutils literal notranslate"><span class="pre">FrameInfo(frame,</span> <span class="pre">filename,</span> <span class="pre">lineno,</span> <span class="pre">function,</span> <span class="pre">code_context,</span> <span class="pre">index)</span></code> is returned.</p> </div> </dd></dl> </section> <section id="fetching-attributes-statically"> <h2>Fetching attributes statically<a class="headerlink" href="#fetching-attributes-statically" title="Permalink to this headline">¶</a></h2> <p>Both <a class="reference internal" href="functions.html#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a> and <a class="reference internal" href="functions.html#hasattr" title="hasattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">hasattr()</span></code></a> can trigger code execution when fetching or checking for the existence of attributes. Descriptors, like properties, will be invoked and <code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattribute__()</span></code> may be called.</p> <p>For cases where you want passive introspection, like documentation tools, this can be inconvenient. <a class="reference internal" href="#inspect.getattr_static" title="inspect.getattr_static"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr_static()</span></code></a> has the same signature as <a class="reference internal" href="functions.html#getattr" title="getattr"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr()</span></code></a> but avoids executing code when it fetches attributes.</p> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getattr_static"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getattr_static</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">attr</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">default</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getattr_static" title="Permalink to this definition">¶</a></dt> <dd><p>Retrieve attributes without triggering dynamic lookup via the descriptor protocol, <code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattr__()</span></code> or <code class="xref py py-meth docutils literal notranslate"><span class="pre">__getattribute__()</span></code>.</p> <p>Note: this function may not be able to retrieve all attributes that getattr can fetch (like dynamically created attributes) and may find attributes that getattr can’t (like descriptors that raise AttributeError). It can also return descriptors objects instead of instance members.</p> <p>If the instance <a class="reference internal" href="stdtypes.html#object.__dict__" title="object.__dict__"><code class="xref py py-attr docutils literal notranslate"><span class="pre">__dict__</span></code></a> is shadowed by another member (for example a property) then this function will be unable to find instance members.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd></dl> <p><a class="reference internal" href="#inspect.getattr_static" title="inspect.getattr_static"><code class="xref py py-func docutils literal notranslate"><span class="pre">getattr_static()</span></code></a> does not resolve descriptors, for example slot descriptors or getset descriptors on objects implemented in C. The descriptor object is returned instead of the underlying attribute.</p> <p>You can handle these with code like the following. Note that for arbitrary getset descriptors invoking these may trigger code execution:</p> <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="c1"># example code for resolving the builtin descriptor types</span> <span class="k">class</span> <span class="nc">_foo</span><span class="p">:</span> <span class="vm">__slots__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'foo'</span><span class="p">]</span> <span class="n">slot_descriptor</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="n">_foo</span><span class="o">.</span><span class="n">foo</span><span class="p">)</span> <span class="n">getset_descriptor</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="nb">type</span><span class="p">(</span><span class="nb">open</span><span class="p">(</span><span class="vm">__file__</span><span class="p">))</span><span class="o">.</span><span class="n">name</span><span class="p">)</span> <span class="n">wrapper_descriptor</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="nb">str</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="s1">'__add__'</span><span class="p">])</span> <span class="n">descriptor_types</span> <span class="o">=</span> <span class="p">(</span><span class="n">slot_descriptor</span><span class="p">,</span> <span class="n">getset_descriptor</span><span class="p">,</span> <span class="n">wrapper_descriptor</span><span class="p">)</span> <span class="n">result</span> <span class="o">=</span> <span class="n">getattr_static</span><span class="p">(</span><span class="n">some_object</span><span class="p">,</span> <span class="s1">'foo'</span><span class="p">)</span> <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">result</span><span class="p">)</span> <span class="ow">in</span> <span class="n">descriptor_types</span><span class="p">:</span> <span class="k">try</span><span class="p">:</span> <span class="n">result</span> <span class="o">=</span> <span class="n">result</span><span class="o">.</span><span class="fm">__get__</span><span class="p">()</span> <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span> <span class="c1"># descriptors can raise AttributeError to</span> <span class="c1"># indicate there is no underlying value</span> <span class="c1"># in which case the descriptor itself will</span> <span class="c1"># have to do</span> <span class="k">pass</span> </pre></div> </div> </section> <section id="current-state-of-generators-and-coroutines"> <h2>Current State of Generators and Coroutines<a class="headerlink" href="#current-state-of-generators-and-coroutines" title="Permalink to this headline">¶</a></h2> <p>When implementing coroutine schedulers and for other advanced uses of generators, it is useful to determine whether a generator is currently executing, is waiting to start or resume or execution, or has already terminated. <a class="reference internal" href="#inspect.getgeneratorstate" title="inspect.getgeneratorstate"><code class="xref py py-func docutils literal notranslate"><span class="pre">getgeneratorstate()</span></code></a> allows the current state of a generator to be determined easily.</p> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getgeneratorstate"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getgeneratorstate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">generator</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getgeneratorstate" title="Permalink to this definition">¶</a></dt> <dd><p>Get current state of a generator-iterator.</p> <dl class="simple"> <dt>Possible states are:</dt><dd><ul class="simple"> <li><p>GEN_CREATED: Waiting to start execution.</p></li> <li><p>GEN_RUNNING: Currently being executed by the interpreter.</p></li> <li><p>GEN_SUSPENDED: Currently suspended at a yield expression.</p></li> <li><p>GEN_CLOSED: Execution has completed.</p></li> </ul> </dd> </dl> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.2.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getcoroutinestate"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getcoroutinestate</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">coroutine</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getcoroutinestate" title="Permalink to this definition">¶</a></dt> <dd><p>Get current state of a coroutine object. The function is intended to be used with coroutine objects created by <a class="reference internal" href="../reference/compound_stmts.html#async-def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code></a> functions, but will accept any coroutine-like object that has <code class="docutils literal notranslate"><span class="pre">cr_running</span></code> and <code class="docutils literal notranslate"><span class="pre">cr_frame</span></code> attributes.</p> <dl class="simple"> <dt>Possible states are:</dt><dd><ul class="simple"> <li><p>CORO_CREATED: Waiting to start execution.</p></li> <li><p>CORO_RUNNING: Currently being executed by the interpreter.</p></li> <li><p>CORO_SUSPENDED: Currently suspended at an await expression.</p></li> <li><p>CORO_CLOSED: Execution has completed.</p></li> </ul> </dd> </dl> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd></dl> <p>The current internal state of the generator can also be queried. This is mostly useful for testing purposes, to ensure that internal state is being updated as expected:</p> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getgeneratorlocals"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getgeneratorlocals</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">generator</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getgeneratorlocals" title="Permalink to this definition">¶</a></dt> <dd><p>Get the mapping of live local variables in <em>generator</em> to their current values. A dictionary is returned that maps from variable names to values. This is the equivalent of calling <a class="reference internal" href="functions.html#locals" title="locals"><code class="xref py py-func docutils literal notranslate"><span class="pre">locals()</span></code></a> in the body of the generator, and all the same caveats apply.</p> <p>If <em>generator</em> is a <a class="reference internal" href="../glossary.html#term-generator"><span class="xref std std-term">generator</span></a> with no currently associated frame, then an empty dictionary is returned. <a class="reference internal" href="exceptions.html#TypeError" title="TypeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">TypeError</span></code></a> is raised if <em>generator</em> is not a Python generator object.</p> <div class="impl-detail compound"> <p><strong>CPython implementation detail:</strong> This function relies on the generator exposing a Python stack frame for introspection, which isn’t guaranteed to be the case in all implementations of Python. In such cases, this function will always return an empty dictionary.</p> </div> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.3.</span></p> </div> </dd></dl> <dl class="py function"> <dt class="sig sig-object py" id="inspect.getcoroutinelocals"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">getcoroutinelocals</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">coroutine</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#inspect.getcoroutinelocals" title="Permalink to this definition">¶</a></dt> <dd><p>This function is analogous to <a class="reference internal" href="#inspect.getgeneratorlocals" title="inspect.getgeneratorlocals"><code class="xref py py-func docutils literal notranslate"><span class="pre">getgeneratorlocals()</span></code></a>, but works for coroutine objects created by <a class="reference internal" href="../reference/compound_stmts.html#async-def"><code class="xref std std-keyword docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code></a> functions.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd></dl> </section> <section id="code-objects-bit-flags"> <span id="inspect-module-co-flags"></span><h2>Code Objects Bit Flags<a class="headerlink" href="#code-objects-bit-flags" title="Permalink to this headline">¶</a></h2> <p>Python code objects have a <code class="docutils literal notranslate"><span class="pre">co_flags</span></code> attribute, which is a bitmap of the following flags:</p> <dl class="py data"> <dt class="sig sig-object py" id="inspect.CO_OPTIMIZED"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_OPTIMIZED</span></span><a class="headerlink" href="#inspect.CO_OPTIMIZED" title="Permalink to this definition">¶</a></dt> <dd><p>The code object is optimized, using fast locals.</p> </dd></dl> <dl class="py data"> <dt class="sig sig-object py" id="inspect.CO_NEWLOCALS"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_NEWLOCALS</span></span><a class="headerlink" href="#inspect.CO_NEWLOCALS" title="Permalink to this definition">¶</a></dt> <dd><p>If set, a new dict will be created for the frame’s <code class="docutils literal notranslate"><span class="pre">f_locals</span></code> when the code object is executed.</p> </dd></dl> <dl class="py data"> <dt class="sig sig-object py" id="inspect.CO_VARARGS"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_VARARGS</span></span><a class="headerlink" href="#inspect.CO_VARARGS" title="Permalink to this definition">¶</a></dt> <dd><p>The code object has a variable positional parameter (<code class="docutils literal notranslate"><span class="pre">*args</span></code>-like).</p> </dd></dl> <dl class="py data"> <dt class="sig sig-object py" id="inspect.CO_VARKEYWORDS"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_VARKEYWORDS</span></span><a class="headerlink" href="#inspect.CO_VARKEYWORDS" title="Permalink to this definition">¶</a></dt> <dd><p>The code object has a variable keyword parameter (<code class="docutils literal notranslate"><span class="pre">**kwargs</span></code>-like).</p> </dd></dl> <dl class="py data"> <dt class="sig sig-object py" id="inspect.CO_NESTED"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_NESTED</span></span><a class="headerlink" href="#inspect.CO_NESTED" title="Permalink to this definition">¶</a></dt> <dd><p>The flag is set when the code object is a nested function.</p> </dd></dl> <dl class="py data"> <dt class="sig sig-object py" id="inspect.CO_GENERATOR"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_GENERATOR</span></span><a class="headerlink" href="#inspect.CO_GENERATOR" title="Permalink to this definition">¶</a></dt> <dd><p>The flag is set when the code object is a generator function, i.e. a generator object is returned when the code object is executed.</p> </dd></dl> <dl class="py data"> <dt class="sig sig-object py" id="inspect.CO_NOFREE"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_NOFREE</span></span><a class="headerlink" href="#inspect.CO_NOFREE" title="Permalink to this definition">¶</a></dt> <dd><p>The flag is set if there are no free or cell variables.</p> </dd></dl> <dl class="py data"> <dt class="sig sig-object py" id="inspect.CO_COROUTINE"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_COROUTINE</span></span><a class="headerlink" href="#inspect.CO_COROUTINE" title="Permalink to this definition">¶</a></dt> <dd><p>The flag is set when the code object is a coroutine function. When the code object is executed it returns a coroutine object. See <span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0492"><strong>PEP 492</strong></a> for more details.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd></dl> <dl class="py data"> <dt class="sig sig-object py" id="inspect.CO_ITERABLE_COROUTINE"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_ITERABLE_COROUTINE</span></span><a class="headerlink" href="#inspect.CO_ITERABLE_COROUTINE" title="Permalink to this definition">¶</a></dt> <dd><p>The flag is used to transform generators into generator-based coroutines. Generator objects with this flag can be used in <code class="docutils literal notranslate"><span class="pre">await</span></code> expression, and can <code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span></code> coroutine objects. See <span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0492"><strong>PEP 492</strong></a> for more details.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.5.</span></p> </div> </dd></dl> <dl class="py data"> <dt class="sig sig-object py" id="inspect.CO_ASYNC_GENERATOR"> <span class="sig-prename descclassname"><span class="pre">inspect.</span></span><span class="sig-name descname"><span class="pre">CO_ASYNC_GENERATOR</span></span><a class="headerlink" href="#inspect.CO_ASYNC_GENERATOR" title="Permalink to this definition">¶</a></dt> <dd><p>The flag is set when the code object is an asynchronous generator function. When the code object is executed it returns an asynchronous generator object. See <span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0525"><strong>PEP 525</strong></a> for more details.</p> <div class="versionadded"> <p><span class="versionmodified added">New in version 3.6.</span></p> </div> </dd></dl> <div class="admonition note"> <p class="admonition-title">Note</p> <p>The flags are specific to CPython, and may not be defined in other Python implementations. Furthermore, the flags are an implementation detail, and can be removed or deprecated in future Python releases. It’s recommended to use public APIs from the <a class="reference internal" href="#module-inspect" title="inspect: Extract information and source code from live objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code></a> module for any introspection needs.</p> </div> </section> <section id="command-line-interface"> <span id="inspect-module-cli"></span><h2>Command Line Interface<a class="headerlink" href="#command-line-interface" title="Permalink to this headline">¶</a></h2> <p>The <a class="reference internal" href="#module-inspect" title="inspect: Extract information and source code from live objects."><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code></a> module also provides a basic introspection capability from the command line.</p> <p>By default, accepts the name of a module and prints the source of that module. A class or function within the module can be printed instead by appended a colon and the qualified name of the target object.</p> <dl class="std cmdoption"> <dt class="sig sig-object std" id="cmdoption-inspect-details"> <span class="sig-name descname"><span class="pre">--details</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-inspect-details" title="Permalink to this definition">¶</a></dt> <dd><p>Print information about the specified object rather than the source code</p> </dd></dl> </section> </section> <div class="clearer"></div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table of Contents</a></h3> <ul> <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code> — Inspect live objects</a><ul> <li><a class="reference internal" href="#types-and-members">Types and members</a></li> <li><a class="reference internal" href="#retrieving-source-code">Retrieving source code</a></li> <li><a class="reference internal" href="#introspecting-callables-with-the-signature-object">Introspecting callables with the Signature object</a></li> <li><a class="reference internal" href="#classes-and-functions">Classes and functions</a></li> <li><a class="reference internal" href="#the-interpreter-stack">The interpreter stack</a></li> <li><a class="reference internal" href="#fetching-attributes-statically">Fetching attributes statically</a></li> <li><a class="reference internal" href="#current-state-of-generators-and-coroutines">Current State of Generators and Coroutines</a></li> <li><a class="reference internal" href="#code-objects-bit-flags">Code Objects Bit Flags</a></li> <li><a class="reference internal" href="#command-line-interface">Command Line Interface</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="gc.html" title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">gc</span></code> — Garbage Collector interface</a></p> <h4>Next topic</h4> <p class="topless"><a href="site.html" title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">site</span></code> — Site-specific configuration hook</a></p> <div role="note" aria-label="source link"> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li> <a href="https://github.com/python/cpython/blob/3.10/Doc/library/inspect.rst" rel="nofollow">Show Source </a> </li> </ul> </div> </div> </div> <div class="clearer"></div> </div> <div class="related" role="navigation" aria-label="related navigation"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="site.html" title="site — Site-specific configuration hook" >next</a> |</li> <li class="right" > <a href="gc.html" title="gc — Garbage Collector interface" >previous</a> |</li> <li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="https://www.python.org/">Python</a> »</li> <li class="switchers"> <div class="language_switcher_placeholder"></div> <div class="version_switcher_placeholder"></div> </li> <li> </li> <li id="cpython-language-and-version"> <a href="../index.html">3.10.12 Documentation</a> » </li> <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li> <li class="nav-item nav-item-2"><a href="python.html" >Python Runtime Services</a> »</li> <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">inspect</span></code> — Inspect live objects</a></li> <li class="right"> <div class="inline-search" role="search"> <form class="inline-search" action="../search.html" method="get"> <input placeholder="Quick search" aria-label="Quick search" type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> | </li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 2001-2026, Python Software Foundation. <br /> This page is licensed under the Python Software Foundation License Version 2. <br /> Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License. <br /> See <a href="/license.html">History and License</a> for more information.<br /> <br /> The Python Software Foundation is a non-profit corporation. <a href="https://www.python.org/psf/donations/">Please donate.</a> <br /> <br /> Last updated on January 26, 2026. <a href="/bugs.html">Found a bug</a>? <br /> Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.3.2. </div> </body> </html>
Close