inspect --- 掻åäžã®ãªããžã§ã¯ãã調æ»ãã¶
ãœãŒã¹ã³ãŒã: Lib/inspect.py
The inspect 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.
ãã®ã¢ãžã¥ãŒã«ã®æ©èœã¯4çš®é¡ã«åé¡ããããšãã§ããŸããåãã§ãã¯ããœãŒã¹ã³ãŒãã®æ å ±ååŸãã¯ã©ã¹ã颿°ããã®æ å ±ååŸãã€ã³ã¿ãŒããªã¿ã®ã¹ã¿ãã¯æ å ±ã®èª¿æ»ã§ãã
åãšã¡ã³ããŒÂ¶
getmembers() ã¯ãã¯ã©ã¹ãã¢ãžã¥ãŒã«ãªã©ã®ãªããžã§ã¯ãããã¡ã³ããŒãååŸããŸããååã "is" ã§å§ãŸã颿°ã¯ã䞻㫠getmembers() ã®ç¬¬2åŒæ°ãšããŠå©çšããããã«æäŸãããŠããŸãã以äžã®ãããªç¹æ®å±æ§ãåç
§ã§ãããã©ãã調ã¹ãæã«ã䜿ããã§ããã (ã¢ãžã¥ãŒã«å±æ§ã«ã€ããŠã¯ Import-related attributes on module objects ãåç
§ããŠãã ãã):
å |
屿§ |
説æ |
|---|---|---|
ã¯ã©ã¹ |
__doc__ |
ããã¥ã¡ã³ãæåå |
__name__ |
ã¯ã©ã¹ã®å®çŸ©å |
|
__qualname__ |
qualified name |
|
__module__ |
ã¯ã©ã¹ãå®çŸ©ããŠããã¢ãžã¥ãŒã«ã®åå |
|
__type_params__ |
ãžã§ããªãã¯ã¯ã©ã¹ã® åãã©ã¡ãŒã¿ (type parameters) ãå«ãã¿ãã« |
|
ã¡ãœãã |
__doc__ |
ããã¥ã¡ã³ãæåå |
__name__ |
ã¡ãœããã®å®çŸ©å |
|
__qualname__ |
qualified name |
|
__func__ |
ã¡ãœãããå®è£ ããŠãã颿°ãªããžã§ã¯ã |
|
__self__ |
ã¡ãœããã«çµåããŠããã€ã³ã¹ã¿ã³ã¹ããŸã㯠|
|
__module__ |
ãã®ã¡ãœãããå®çŸ©ãããŠããã¢ãžã¥ãŒã«ã®åå |
|
颿° |
__doc__ |
ããã¥ã¡ã³ãæåå |
__name__ |
颿°ã®å®çŸ©å |
|
__qualname__ |
qualified name |
|
__code__ |
颿°ãã³ã³ãã€ã«ãã ãã€ãã³ãŒã ãæ ŒçŽããã³ãŒããªããžã§ã¯ã |
|
__defaults__ |
äœçœ®ãŸãã¯ããŒã¯ãŒãåŒæ°ã®å šãŠã®æ¢å®å€ã®ã¿ãã« |
|
__kwdefaults__ |
ããŒã¯ãŒãå°çšåŒæ°ã®å šãŠã®æ¢å®å€ã®ãããã³ã° |
|
__globals__ |
颿°ãå®çŸ©ãããã°ããŒãã«åå空é |
|
__builtins__ |
çµã¿èŸŒã¿ã¢ãžã¥ãŒã«ã®åå空é |
|
__annotations__ |
ä»®åŒæ°åããã¢ãããŒã·ã§ã³ãžã®ãããã³ã°; |
|
__type_params__ |
ãžã§ããªãã¯é¢æ°ã® åãã©ã¡ãŒã¿ (type parameters) ãå«ãã¿ãã« |
|
__module__ |
ãã®é¢æ°ãå®çŸ©ãããŠããã¢ãžã¥ãŒã«ã®åå |
|
traceback |
tb_frame |
ãã®ã¬ãã«ã®ãã¬ãŒã ãªããžã§ã¯ã |
tb_lasti |
æåŸã«å®è¡ããããšãããã€ãã³ãŒãäžã®ã€ã³ã¹ãã©ã¯ã·ã§ã³ã瀺ãã€ã³ããã¯ã¹ |
|
tb_lineno |
çŸåšã® Python ãœãŒã¹ã³ãŒãã®è¡çªå· |
|
tb_next |
ãã®ãªããžã§ã¯ãã®å åŽ (ãã®ã¬ãã«ããåŒã³åºããã) ã®ãã¬ãŒã¹ããã¯ãªããžã§ã¯ã |
|
ãã¬ãŒã |
f_back |
å€åŽ (ãã®ãã¬ãŒã ãåŒã³åºãã) ã®ãã¬ãŒã ãªããžã§ã¯ã |
f_builtins |
ãã®ãã¬ãŒã ã§åç §ããŠããçµã¿èŸŒã¿åå空é |
|
f_code |
ãã®ãã¬ãŒã ã§å®è¡ããŠããã³ãŒããªããžã§ã¯ã |
|
f_globals |
ãã®ãã¬ãŒã ã§åç §ããŠããã°ããŒãã«åå空é |
|
f_lasti |
æåŸã«å®è¡ããããšãããã€ãã³ãŒãäžã®ã€ã³ã¹ãã©ã¯ã·ã§ã³ã瀺ãã€ã³ããã¯ã¹ |
|
f_lineno |
çŸåšã® Python ãœãŒã¹ã³ãŒãã®è¡çªå· |
|
f_locals |
ãã®ãã¬ãŒã ã§åç §ããŠããããŒã«ã«åå空é |
|
f_generator |
returns the generator or
coroutine object that
owns this frame, or
|
|
f_trace |
ãã®ãã¬ãŒã ã®ãã¬ãŒã¹é¢æ°ããŸã㯠|
|
f_trace_lines |
indicate whether a tracing event is triggered for each source source line |
|
f_trace_opcodes |
indicate whether per-opcode events are requested |
|
clear() |
used to clear all references to local variables |
|
ã³ãŒã |
co_argcount |
åŒæ°ã®æ° (ããŒã¯ãŒãéå®åŒæ°ãããã³ * ã ** ã¯å«ãŸãªã) |
co_code |
ã³ã³ãã€ã«ããããã€ãã³ãŒããã®ãŸãŸã®æåå |
|
co_cellvars |
(èªèº«ãå å«ããã¹ã³ãŒãããåç §ããã) ã»ã«å€æ°ã®ååã®ã¿ãã« |
|
co_consts |
ãã€ãã³ãŒãäžã§äœ¿çšããŠãã宿°ã®ã¿ãã« |
|
co_filename |
ã³ãŒããªããžã§ã¯ããçæãããã¡ã€ã«ã®ãã¡ã€ã«å |
|
co_firstlineno |
Python ãœãŒã¹ã³ãŒãã®å é è¡ |
|
co_flags |
|
|
co_lnotab |
encoded mapping of line numbers to bytecode indices |
|
co_freevars |
(颿°ã®ã¯ããŒãžã£ãä»ããŠåç §ããã) èªç±å€æ°ã®ååã®ã¿ãã« |
|
co_posonlyargcount |
äœçœ®å°çšåŒæ°ã®æ° |
|
co_kwonlyargcount |
ããŒã¯ãŒãå°çšåŒæ° (** åŒæ°ãå«ãŸãªã) ã®æ° |
|
co_name |
ã³ãŒããªããžã§ã¯ããå®çŸ©ããããšãã®åå |
|
co_qualname |
ãã®ã³ãŒããªããžã§ã¯ããå®çŸ©ããããšãã®å®å šä¿®é£Ÿå (fully qualified name) |
|
co_names |
颿°ã®åŒæ°ã§ãããŒã«ã«å€æ°ã§ããªãååã®ã¿ãã« |
|
co_nlocals |
ããŒã«ã«å€æ°ã®æ° |
|
co_stacksize |
å¿ èŠãšãããä»®æ³ãã·ã³ã®ã¹ã¿ãã¯ã¹ããŒã¹ |
|
co_varnames |
åŒæ°åãšããŒã«ã«å€æ°åã®ã¿ãã« |
|
co_lines() |
returns an iterator that yields successive bytecode ranges |
|
co_positions() |
returns an iterator of source code positions for each bytecode instruction |
|
replace() |
returns a copy of the code object with new values |
|
ãžã§ãã¬ãŒã¿ |
__name__ |
name |
__qualname__ |
qualified name |
|
gi_frame |
ãã¬ãŒã |
|
gi_running |
ãžã§ãã¬ãŒã¿ãå®è¡äžãã©ãã |
|
gi_suspended |
is the generator suspended? |
|
gi_code |
ã³ãŒã |
|
gi_yieldfrom |
|
|
éåæãžã§ãã¬ãŒã¿ |
__name__ |
name |
__qualname__ |
qualified name |
|
ag_await |
åŸ
æ©ãããŠãããªããžã§ã¯ãããŸã㯠|
|
ag_frame |
ãã¬ãŒã |
|
ag_running |
ãžã§ãã¬ãŒã¿ãå®è¡äžãã©ãã |
|
ag_suspended |
is the generator suspended? |
|
ag_code |
ã³ãŒã |
|
ã³ã«ãŒãã³ |
__name__ |
name |
__qualname__ |
qualified name |
|
cr_await |
åŸ
æ©ãããŠãããªããžã§ã¯ãããŸã㯠|
|
cr_frame |
ãã¬ãŒã |
|
cr_running |
ã³ã«ãŒãã³ãå®è¡äžãã©ãã |
|
cr_suspended |
is the coroutine suspended? |
|
cr_code |
ã³ãŒã |
|
cr_origin |
|
|
çµã¿èŸŒã¿ |
__doc__ |
ããã¥ã¡ã³ãæåå |
__name__ |
颿°ãã¡ãœããã®å ã ã®åå |
|
__qualname__ |
qualified name |
|
__self__ |
ã¡ãœãããçµåããŠããã€ã³ã¹ã¿ã³ã¹ããŸã㯠|
ããŒãžã§ã³ 3.5 ã§å€æŽ: ãžã§ãã¬ãŒã¿ã« __qualname__ ãš gi_yieldfrom 屿§ã远å ãããŸããã
ãžã§ãã¬ãŒã¿ã® __name__ 屿§ãã³ãŒãåã§ã¯ãªã颿°åããèšå®ãããããã«ãªãã倿Žã§ããããã«ãªããŸããã
ããŒãžã§ã³ 3.7 ã§å€æŽ: ã³ã«ãŒãã³ã« cr_origin 屿§ã远å ããŸããã
ããŒãžã§ã³ 3.10 ã§å€æŽ: 颿°ã« __builtins__ 屿§ã远å ããŸããã
ããŒãžã§ã³ 3.11 ã§å€æŽ: Add gi_suspended attribute to generators.
ããŒãžã§ã³ 3.11 ã§å€æŽ: Add cr_suspended attribute to coroutines.
ããŒãžã§ã³ 3.12 ã§å€æŽ: Add ag_suspended attribute to async generators.
ããŒãžã§ã³ 3.14 ã§å€æŽ: Add f_generator attribute to frames.
- inspect.getmembers(object[, predicate])¶
ãªããžã§ã¯ãã®å šãŠã®ã¡ã³ããŒã
(name, value)ãã¢ã®ãªã¹ãã§è¿ããŸãããªã¹ãã¯åå (name) ã§ãœãŒããããŸãããªãã·ã§ã³åŒæ° predicate ãæå®ãããå Žåãåã¡ã³ããŒã®valueãªããžã§ã¯ããåŒæ°ãšã㊠predicate ãåŒã°ãããã®æ»ãå€ãçãšãªããšãªãã¡ã³ããŒã ãããªã¹ãã«å«ãŸããŸããæ³šé
åŒæ°ãã¯ã©ã¹ã§ãã®å±æ§ãã¡ã¿ã¯ã©ã¹ã§ç¹å¥ã«å®çŸ©ããã
__dir__()ã«åæãããŠããå Žåãgetmembers()ã¯ã¡ã¿ã¯ã©ã¹ã§å®çŸ©ãããã¯ã©ã¹å±æ§ã®ã¿ãè¿ããŸãã
- inspect.getmembers_static(object[, predicate])¶
ãªããžã§ã¯ãã®å šãŠã®ã¡ã³ããŒã
(name, value)ãã¢ã®ãªã¹ãã§è¿ããŸãããã ãããã¹ã¯ãªãã¿ãããã³ã«ã® __getattr__ ã __getattribute__ ãä»ããåçãªã«ãã¯ã¢ããã¯è¡ããŸããããªãã·ã§ã³ãšããŠãåŒæ°ã«äžãããã predicate ãæºããã¡ã³ããŒã®ã¿ãè¿ãããšãã§ããŸããæ³šé
getmembers_static()㯠getmembers ãååŸã§ããã¡ã³ããŒã®å šãŠãåãåºãããšã¯ã§ããªããããããŸãã (ããšãã°åçã«çæããã屿§ãªã©)ããéã« getmembers ãèŠã€ããããšã®ã§ããªãã£ãã¡ã³ããŒãååŸã§ããããšããããŸã (AttributeError ãéåºãããã¹ã¯ãªãã¿ãªã©)ããŸããæã«ã¯ã€ã³ã¹ã¿ã³ã¹ã¡ã³ããŒã®ä»£ããã«ãã¹ã¯ãªãã¿ãªããžã§ã¯ããè¿ãããšããããŸããAdded in version 3.11.
- inspect.getmodulename(path)¶
ãã¡ã€ã« path ã§æå®ãããã¢ãžã¥ãŒã«ã®ååãããã®ã¢ãžã¥ãŒã«ãå«ãããã±ãŒãžã®ååãå«ãŸãªã圢ã§è¿ããŸãããã¡ã€ã«æ¡åŒµåã¯ã
importlib.machinery.all_suffixes()ã®å šãŠã®ãšã³ããªã«å¯ŸããŠäžèŽãããã©ããããã§ãã¯ãããŸããæ¡åŒµåãäžèŽããå ŽåãæåŸã®ãã¹æåããæ¡åŒµåãé€ãããã®ãè¿ããŸãããã以å€ã®å Žåã¯Noneãè¿ããŸãããã®é¢æ°ã¯ãå®éã® Python ã¢ãžã¥ãŒã«ãšããŠæå³ã®ããåå ã ã ãè¿ããŸããããªãã¡ã Python ããã±ãŒãžãæããŠããå¯èœæ§ã®ãããã¹ã«å¯ŸããŠã¯ãäŸç¶ãšããŠ
Noneãè¿ããŸããããŒãžã§ã³ 3.3 ã§å€æŽ: ãã®é¢æ°ã¯çŽæ¥
importlibã«äŸåããããã«ãªããŸããã
- inspect.ismodule(object)¶
ãªããžã§ã¯ããã¢ãžã¥ãŒã«ã®å Žå
Trueãè¿ããŸãã
- inspect.isclass(object)¶
ãªããžã§ã¯ããçµã¿èŸŒã¿ã Python ãçæããã¯ã©ã¹ã®å Žåã«
Trueãè¿ããŸããThis function returns
Falsefor generic aliases of classes, such aslist[int].
- inspect.ismethod(object)¶
ãªããžã§ã¯ããã¡ãœããã®å Žå
Trueãè¿ããŸããæ³šé
For example, given this class:
>>> class Greeter: ... def say_hello(self): ... print('hello!')
A bound method (also known as an instance method) is created when accessing
say_hello(a function defined in theGreeternamespace) through an instance of theGreeterclass:>>> instance = Greeter() >>> instance.say_hello <bound method Greeter.say_hello of <__main__.Greeter object ...>> >>> ismethod(instance.say_hello) True >>> isfunction(instance.say_hello) False
Accessing
say_hellothrough theGreeterclass will return the function itself. For this function,ismethod()will returnFalse, butisfunction()will returnTrue:>>> Greeter.say_hello <function Greeter.say_hello at 0x7f7503854a90> >>> ismethod(Greeter.say_hello) False >>> isfunction(Greeter.say_hello) True
See ã¡ãœãã for details.
- inspect.isfunction(object)¶
ãªããžã§ã¯ããã lambda åŒã§çæããããã®ãå«ã Python 颿°ã®å Žåã«
Trueãè¿ããŸããSee the note for
ismethod()for an example.
- inspect.isgeneratorfunction(object)¶
ãªããžã§ã¯ãã Python ã®ãžã§ãã¬ãŒã¿é¢æ°ã®å Žå
Trueãè¿ããŸããIt also returns
Truefor bound methods created from Python generator functions (see ã¡ãœãã for more information).ããŒãžã§ã³ 3.8 ã§å€æŽ:
functools.partial()ã§ã©ãããã颿°ã«å¯ŸããŠãã©ããããã颿°ã Python ã®ãžã§ãã¬ãŒã¿é¢æ°ã§ããå Žåã¯Trueãè¿ãããã«ãªããŸãããããŒãžã§ã³ 3.10.6 ã§å€æŽ: Duck-typed function-like objects now return
Trueif their code object has theCO_GENERATORflag.ããŒãžã§ã³ 3.13 ã§å€æŽ:
functools.partialmethod()ã§ã©ãããã颿°ã«å¯ŸããŠãã©ããããã颿°ã Python ã®ãžã§ãã¬ãŒã¿é¢æ°ã§ããå Žåã¯Trueãè¿ãããã«ãªããŸããã
- inspect.isgenerator(object)¶
ãªããžã§ã¯ãããžã§ãã¬ãŒã¿ã®å Žå
Trueãè¿ããŸãã
- inspect.iscoroutinefunction(object)¶
ãªããžã§ã¯ããã³ã«ãŒãã³é¢æ° coroutine function (
async defæ§æã§å®çŸ©ããã颿°)ãã³ã«ãŒãã³é¢æ° coroutine function ãã©ããããŠããfunctools.partial()ãããã¯markcoroutinefunction()ãšããŒã¯ãããåæé¢æ°ã®ããããã®å Žåã«Trueãè¿ããŸãAdded in version 3.5.
ããŒãžã§ã³ 3.8 ã§å€æŽ:
functools.partial()ã§ã©ãããã颿°ã«å¯ŸããŠãã©ããããã颿°ã ã³ã«ãŒãã³é¢æ° coroutine function ã§ããå Žåã¯Trueãè¿ãããã«ãªããŸãããããŒãžã§ã³ 3.10.6 ã§å€æŽ: Duck-typed function-like objects now return
Trueif their code object has theCO_COROUTINEflag.ããŒãžã§ã³ 3.12 ã§å€æŽ:
markcoroutinefunction()ãšããŒã¯ãããåæé¢æ°ã«å¯ŸããŠTrueãè¿ãããã«ãªããŸãããããŒãžã§ã³ 3.13 ã§å€æŽ:
functools.partialmethod()ã§ã©ãããã颿°ã«å¯ŸããŠãã©ããããã颿°ãã³ã«ãŒãã³é¢æ° coroutine function ã§ããå Žåã¯Trueãè¿ãããã«ãªããŸããã
- inspect.markcoroutinefunction(func)¶
ãã®ãŸãŸã§ããã°
iscoroutinefunction()ãã³ã«ãŒãã³é¢æ°ãšå€å®ããªããããªåŒã³åºãå¯èœãªããžã§ã¯ãããã³ã«ãŒãã³é¢æ° coroutine function ãšããŒã¯ããããã®ãã³ã¬ãŒã¿ã§ããããã¯ãã³ã«ãŒãã³ coroutine ãè¿ãåæé¢æ°ãã
iscoroutinefunction()ãçã§ããããšãèŠæ±ããAPIã«æž¡ãããå Žåã«åœ¹ã«ç«ã€ãããããŸãããå¯èœãªãã
async defã䜿ã£ã颿°å®çŸ©ã奜ãŸããã§ãããŸãã颿°ãåŒã³åºããäžã§ãã®æ»ãå€ãiscoroutine()ã§ãã¹ãããæ¹æ³ã容èªã§ããŸããAdded in version 3.12.
- inspect.iscoroutine(object)¶
ãªããžã§ã¯ãã
async defã§çæããã ã³ã«ãŒãã³ ã®å ŽåTrueãè¿ããŸããAdded in version 3.5.
- inspect.isawaitable(object)¶
ãªããžã§ã¯ãã
awaitåŒå ã§äœ¿çšã§ããå ŽåTrueãè¿ããŸãããžã§ãã¬ãŒã¿ããŒã¹ã®ã³ã«ãŒãã³ãšéåžžã®ãžã§ãã¬ãŒã¿ãåºå¥ããã®ã«ã䜿ããŸã:
import types def gen(): yield @types.coroutine def gen_coro(): yield assert not isawaitable(gen()) assert isawaitable(gen_coro())
Added in version 3.5.
- inspect.isasyncgenfunction(object)¶
ãªããžã§ã¯ããéåæãžã§ãã¬ãŒã¿ asynchronous generator 颿°ã§ããå Žåã«
Trueãè¿ããŸãã䜿çšäŸ:>>> async def agen(): ... yield 1 ... >>> inspect.isasyncgenfunction(agen) True
Added in version 3.6.
ããŒãžã§ã³ 3.8 ã§å€æŽ:
functools.partial()ã§ã©ããããã颿°ã§ããå ã®é¢æ°ãéåæãžã§ãã¬ãŒã¿ asynchronous generator 颿°ã§ããã°Trueãè¿ãããã«ãªããŸãããããŒãžã§ã³ 3.10.6 ã§å€æŽ: Duck-typed function-like objects now return
Trueif their code object has theCO_ASYNC_GENERATORflag.ããŒãžã§ã³ 3.13 ã§å€æŽ: Functions wrapped in
functools.partialmethod()now returnTrueif the wrapped function is a asynchronous generator function.
- inspect.isasyncgen(object)¶
ãªããžã§ã¯ãã asynchronous generator 颿°ã«ãã£ãŠçæããã asynchronous generator iterator ã§ããå Žåã«
Trueãè¿ããŸããAdded in version 3.6.
- inspect.istraceback(object)¶
ãªããžã§ã¯ãããã¬ãŒã¹ããã¯ã®å Žåã¯
Trueãè¿ããŸãã
- inspect.isframe(object)¶
ãªããžã§ã¯ãããã¬ãŒã ã®å Žåã¯
Trueãè¿ããŸãã
- inspect.iscode(object)¶
ãªããžã§ã¯ããã³ãŒãã®å Žåã¯
Trueãè¿ããŸãã
- inspect.isbuiltin(object)¶
ãªããžã§ã¯ããçµã¿èŸŒã¿é¢æ°ãæçžæžã¿ã®çµã¿èŸŒã¿ã¡ãœããã®å Žåã«
Trueãè¿ããŸãã
- inspect.ismethodwrapper(object)¶
ãªããžã§ã¯ãã®åã
MethodWrapperTypeã§ããå Žåã«Trueãè¿ããŸããçãè¿ãã®ã¯
MethodWrapperTypeã®ã€ã³ã¹ã¿ã³ã¹ã§ã__str__(),__eq__(),__repr__()ãªã©ã§ããAdded in version 3.11.
- inspect.isroutine(object)¶
ãªããžã§ã¯ãããŠãŒã¶å®çŸ©ãçµã¿èŸŒã¿ã®é¢æ°ãŸãã¯ã¡ãœããã®å Žåã¯
Trueãè¿ããŸãã
- inspect.isabstract(object)¶
ãªããžã§ã¯ããæœè±¡åºåºã¯ã©ã¹ã§ãããšãã«
Trueãè¿ããŸãã
- inspect.ismethoddescriptor(object)¶
Return
Trueif the object is a method descriptor, but not ifisclass(),ismethod()orisfunction()is true.ããã¯ããšãã°ã
int.__add__ã§çãè¿ããŸãããã®é¢æ°ã«ãããã¹ãããã¹ããïŒçãè¿ãïŒãªããžã§ã¯ãã¯__get__()ã¡ãœãããæã¡ãŸããã__set__()ã¡ãœããã__delete__()ã¡ãœããã¯æã¡ãŸããããã以å€ã®å±æ§ã®æç¡ã¯ããŸããŸã§ãã__name__屿§ã¯æ®éã¯ååšããŸããã__doc__屿§ããã°ãã°èŠãããŸããMethod descriptors that also pass any of the other tests (
isclass(),ismethod()orisfunction()) make this function returnFalse, simply because those other tests promise more -- you can, for example, count on having the__func__attribute when an object passesismethod().ããŒãžã§ã³ 3.13 ã§å€æŽ: ãã®é¢æ°ã¯ã
__get__()屿§ãš__delete__()屿§ãæã¡ã__set__()屿§ãæããªããªããžã§ã¯ããã¡ãœãããã¹ã¯ãªãã¿ã§ãããšäžæ£ã«å ±åããããšã¯ãªããªããŸãã (ãã®ãããªãªããžã§ã¯ãã¯ããŒã¿ãã¹ã¯ãªãã¿ã§ãã£ãŠãã¡ãœãããã¹ã¯ãªãã¿ã§ã¯ãããŸãã)ã
- inspect.isdatadescriptor(object)¶
Return
Trueif the object is a data descriptor, but not ifisclass(),ismethod()orisfunction()is true.Data descriptors always have a
__set__()method and/or a__delete__()method. Optionally, they may also have a__get__()method.Examples of data descriptors are
properties, getsets and member descriptors. Note that for the latter two (defined only in C extension modules), more specific tests are available:isgetsetdescriptor()andismemberdescriptor(), respectively.While data descriptors may also have
__name__and__doc__attributes (as properties, getsets and member descriptors do), this is not necessarily the case in general.ããŒãžã§ã³ 3.8 ã§å€æŽ: This function now reports objects with only a
__set__()method as being data descriptors (the presence of__get__()is no longer required for that). Moreover, objects with__delete__(), but not__set__(), are now properly recognized as data descriptors as well, which was not the case previously.
- inspect.isgetsetdescriptor(object)¶
ãªããžã§ã¯ãã getset ãã¹ã¯ãªãã¿ã®å Žåã«
Trueãè¿ããŸããgetset ãšã¯ãæ¡åŒµã¢ãžã¥ãŒã«ã§
PyGetSetDefæ§é äœãçšããŠå®çŸ©ããã屿§ã®ããšã§ãããã®ãããªåãæããªã Python å®è£ ã®å Žåã¯ããã®ã¡ãœããã¯åžžã«Falseãè¿ããŸãã
- inspect.ismemberdescriptor(object)¶
ãªããžã§ã¯ããã¡ã³ããŒãã¹ã¯ãªãã¿ã®å Žåã«
Trueãè¿ããŸããã¡ã³ããŒãã¹ã¯ãªãã¿ãšã¯ãæ¡åŒµã¢ãžã¥ãŒã«ã§
PyMemberDefæ§é äœãçšããŠå®çŸ©ããã屿§ã®ããšã§ãããã®ãããªåãæããªã Python å®è£ ã®å Žåã¯ããã®ã¡ãœããã¯åžžã«Falseãè¿ããŸãã
ãœãŒã¹ã³ãŒãã®æ å ±ååŸÂ¶
- inspect.getdoc(object)¶
Get the documentation string for an object, cleaned up with
cleandoc(). 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. ReturnNoneif the documentation string is invalid or missing.ããŒãžã§ã³ 3.5 ã§å€æŽ: ããã¥ã¡ã³ããŒã·ã§ã³æååããªãŒããŒã©ã€ããããŠããªãå Žåã¯ç¶æ¿ãããããã«ãªããŸããã
- inspect.getcomments(object)¶
ãªããžã§ã¯ãã®ãœãŒã¹ã³ãŒãçŽå (ã¯ã©ã¹ã颿°ãã¡ãœããã®å Žå) ãŸã㯠Python ãœãŒã¹ãã¡ã€ã«ã®å é (ã¢ãžã¥ãŒã«ã®å Žå) ã®ä»»æã®è¡æ°ã«ãããã³ã¡ã³ãããã²ãšã€ã®æååãšããŠè¿ããŸãããªããžã§ã¯ãã®ãœãŒã¹ã³ãŒããååšããªãå Žåã¯
Noneãè¿ããŸãããªããžã§ã¯ãã C ã§å®çŸ©ãããŠããå Žåãã察話åã·ã§ã«ã§å®çŸ©ãããªããžã§ã¯ãã®å Žåã«ãã®ãããªããšãèµ·ããåŸãŸãã
- inspect.getfile(object)¶
Return the name of the (text or binary) file in which an object was defined. An
OSErroris raised if the source code cannot be retrieved. This will fail with aTypeErrorif the object is a built-in module, class, or function.
- inspect.getmodule(object)¶
ãªããžã§ã¯ããå®çŸ©ãããŠããã¢ãžã¥ãŒã«ã®æšå®ã詊ã¿ãŸããã¢ãžã¥ãŒã«ã決ããããªãå Žåã¯
Noneãè¿ããŸãã
- inspect.getsourcefile(object)¶
Return the name of the Python source file in which an object was defined or
Noneif no way can be identified to get the source. AnOSErroris raised if the source code cannot be retrieved. This will fail with aTypeErrorif the object is a built-in module, class, or function.
- inspect.getsourcelines(object)¶
ãªããžã§ã¯ããå®çŸ©ããŠãããœãŒã¹ã³ãŒãã®è¡ã®ãªã¹ããšãå®çŸ©ã®éå§è¡çªå·ãè¿ããŸããåŒæ°ã«ã¯ã¢ãžã¥ãŒã«ãã¯ã©ã¹ãã¡ãœããã颿°ããã¬ãŒã¹ããã¯ããŸãã¯ã³ãŒããªããžã§ã¯ããæž¡ãããšãã§ããŸãããªããžã§ã¯ãã«å¯Ÿå¿ãããœãŒã¹ã³ãŒããè¡ã®ãªã¹ããšããŠè¿ãããŸãããŸãè¡çªå·ã¯ãå ã®ãœãŒã¹ãã¡ã€ã«äžã§ã³ãŒããèŠã€ãã£ãæåã®è¡ã瀺ããŸãããœãŒã¹ã³ãŒããæ¢ãåºãããšãã§ããªãã£ãå Žåã
OSErroräŸå€ãéåºãããŸãããªããžã§ã¯ããçµã¿èŸŒã¿ã®ã¢ãžã¥ãŒã«ãã¯ã©ã¹ããŸãã¯é¢æ°ã§ããå Žåã¯TypeErroräŸå€ãéåºãããŸãã
- inspect.getsource(object)¶
ãªããžã§ã¯ããå®çŸ©ããŠãããœãŒã¹ã³ãŒãã®ããã¹ããè¿ããŸããåŒæ°ã«ã¯ã¢ãžã¥ãŒã«ãã¯ã©ã¹ãã¡ãœããã颿°ããã¬ãŒã¹ããã¯ããã¬ãŒã ããŸãã¯ã³ãŒããªããžã§ã¯ããæž¡ãããšãã§ããŸãããœãŒã¹ã³ãŒãã¯ã²ãšã€ã®æååãšããŠè¿ãããŸãããœãŒã¹ã³ãŒããæ¢ãåºãããšãã§ããªãã£ãå Žåã
OSErroräŸå€ãéåºãããŸãããªããžã§ã¯ããçµã¿èŸŒã¿ã®ã¢ãžã¥ãŒã«ãã¯ã©ã¹ããŸãã¯é¢æ°ã§ããå Žåã¯TypeErroräŸå€ãéåºãããŸãã
- inspect.cleandoc(doc)¶
ã³ãŒããããã¯ãšäœçœ®ãåãããããã®ã€ã³ãã³ãã docstring ããåé€ããŸãã
å é è¡ã®è¡é ã®ç©ºçœæåã¯å šãŠåé€ãããŸãã 2è¡ç®ä»¥éã§ã¯å šè¡ã§åãæ°ã®è¡é ã®ç©ºçœæåããåé€ã§ããã ãåé€ãããŸãã ãã®åŸãå é ãšæ«å°Ÿã®ç©ºçœè¡ãåé€ãããå šãŠã®ã¿ãã空çœã«å±éãããŸãã
Signature ãªããžã§ã¯ãã§åŒã³åºãå¯èœãªããžã§ã¯ããå çãã¶
Added in version 3.3.
Signature ãªããžã§ã¯ãã¯ãåŒã³åºãå¯èœãªããžã§ã¯ãã®åŒã³åºãã·ã°ããã£ãšæ»ãå€ã®ã¢ãããŒã·ã§ã³ã衚çŸããŸãã Signature ãªããžã§ã¯ããååŸããã«ã¯ã signature() 颿°ã䜿ã£ãŠãã ããã
- inspect.signature(callable, *, follow_wrapped=True, globals=None, locals=None, eval_str=False, annotation_format=Format.VALUE)¶
callable ã§äžãããããªããžã§ã¯ãã«å¯Ÿãã
Signatureãªããžã§ã¯ããè¿ããŸã:>>> from inspect import signature >>> def foo(a, *, b:int, **kwargs): ... pass >>> sig = signature(foo) >>> str(sig) '(a, *, b: int, **kwargs)' >>> str(sig.parameters['b']) 'b: int' >>> sig.parameters['b'].annotation <class 'int'>
åçŽãªé¢æ°ãã¯ã©ã¹ããã
functools.partial()ãªããžã§ã¯ããŸã§ãå¹ åºã Python ã®åŒã³åºãå¯èœãªãªããžã§ã¯ããåãä»ããŸããã¢ãããŒã·ã§ã³ã®ããã€ãã (ããšãã°
from __future__ import annotationsã䜿ãããŠããããšãçç±ã«) æååã§ããå Žåãsignature()ã¯annotationlib.get_annotations()ã䜿ã£ãŠãéæåååãããªãã¡æååã¢ãããŒã·ã§ã³ã解決ããŠæ¬æ¥ã®ã¢ãããŒã·ã§ã³ã«æ»ãããšã詊ã¿ãŸããã¢ãããŒã·ã§ã³ã®è§£æ±ºãè¡ãéã«ã¯ globals, locals, ããã³ eval_str ãã©ã¡ãŒã¿ãannotationlib.get_annotations()ã«æž¡ãããŸã; ãããã®ãã©ã¡ãŒã¿ãäœ¿ãæ¹æ³ã«ã€ããŠã®èª¬æã¯annotationlib.get_annotations()ã®ããã¥ã¡ã³ããŒã·ã§ã³ãåç §ããŠãã ãããè¿ãããã¢ãããŒã·ã§ã³ã®åœ¢åŒãå¶åŸ¡ããããã annotation_format ãã©ã¡ãŒã¿ã«ã¯annotationlib.Formatåæåã®ã¡ã³ããŒãæž¡ãããšãã§ããŸããããšãã°ã¢ãããŒã·ã§ã³ãæåå圢åŒã§è¿ãããã«ã¯annotation_format=annotationlib.Format.STRINGã䜿ã£ãŠãã ãããã·ã°ããã£ãæäŸã§ããªãå Žåã¯
ValueErroräŸå€ãéåºããŸãããŸããªããžã§ã¯ãã®åããµããŒããããŠããªãå Žåã¯TypeErroräŸå€ãéåºããŸããããã«ãã¢ãããŒã·ã§ã³ãæåååãããŠããã eval_str ãåœã§ãªãå Žåãã¢ãããŒã·ã§ã³ããéæåååãããããã«annotationlib.get_annotations()å éšã§åŒã°ããeval()ã«ããããããªãçš®é¡ã®äŸå€ãéåºãããå¯èœæ§ããããŸãã颿°ã·ã°ããã£ã«ãããã¹ã©ãã·ã¥ (/) ã¯ãããããåã®ãã©ã¡ãŒã¿ãäœçœ®å°çšåŒæ°ã§ããããšã瀺ããŸãããã詳ããæ å ±ã¯ã äœçœ®å°çšåŒæ°ã«é¢ãã FAQ ãšã³ã㪠ãåç §ããŠãã ããã
ããŒãžã§ã³ 3.5 ã§å€æŽ: follow_wrapped ãã©ã¡ãŒã¿ã远å ãããŸãããparameter was added. ç¹ã« callable ãã®ãã®ã«ã€ããŠã®ã·ã°ããã£ãååŸããããã«ã¯
Falseãæå®ããŠãã ãã (ããããããšã§ããã³ã¬ãŒã¿ã§ä¿®é£ŸãããåŒã³åºãå¯èœãªããžã§ã¯ãã«å¯ŸããŠ``callable.__wrapped__`` ã䜿ãããªããªããŸã)ãããŒãžã§ã³ 3.10 ã§å€æŽ: globals, locals, ããã³ eval_str ãã©ã¡ãŒã¿ã远å ãããŸããã
ããŒãžã§ã³ 3.14 ã§å€æŽ: annotation_format ãã©ã¡ãŒã¿ã远å ãããŸããã
泚é
ããã€ãã®åŒã³åºãå¯èœãªããžã§ã¯ãã¯ããã Python å®è£ ã®äžã§ã¯ã€ã³ããã¹ãã¯ã·ã§ã³ (å®è¡æãªããžã§ã¯ã調æ») ãã§ããªããããããŸãããããšãã° CPython ã§ã¯ã C ã§å®çŸ©ãããããã€ãã®çµã¿èŸŒã¿é¢æ°ã¯ãããã®åŒæ°ã«é¢ããã¡ã¿ããŒã¿ãæäŸããŸããã
æž¡ããããªããžã§ã¯ãã
__signature__屿§ãæã€å Žåãããã«ããšã¥ããŠã·ã°ããã£ãäœãããšãã§ãããããããŸãããå³å¯ãªã»ãã³ãã£ã¯ã¹ã¯å®è£ ã¬ãã«ã®è©³çްã§ãããäºåãªã倿Žãããå¯èœæ§ããããŸããçŸåšã®ã»ãã³ãã£ã¯ã¹ã«ã€ããŠã¯ãœãŒã¹ã³ãŒãããåç §ããŠãã ããã
- class inspect.Signature(parameters=None, *, return_annotation=Signature.empty)¶
Signatureãªããžã§ã¯ãã¯ã颿°ã®åŒã³åºãã·ã°ããã£ãšæ»ãå€ã®ã¢ãããŒã·ã§ã³ã衚çŸããŸãããã®ãªããžã§ã¯ãã¯parameterséåãšããŠã颿°ãåãåããã©ã¡ãŒã¿ããããã«ã€ããŠã®Parameterãªããžã§ã¯ããä¿åããŸãããªãã·ã§ã³åŒæ° parameters ã¯
Parameterãªããžã§ã¯ãã®ã·ãŒã±ã³ã¹ã§ãéè€ããååã®ãã©ã¡ãŒã¿ã¯ãªããããã©ã¡ãŒã¿ã®äžŠã³é ã¯æ£ããããããªãã¡äœçœ®å°çšåŒæ°ããã¯ããŸã次ã«äœçœ®åŒæ°ãšããŠãããŒã¯ãŒãåŒæ°ãšããŠãæå®å¯èœãªãã©ã¡ãŒã¿ãç¶ããã©ããããŸãããã©ã«ãå€ã®ãããã©ã¡ãŒã¿ãããã©ã«ãå€ã®ãªããã©ã¡ãŒã¿ã®åŸã«ãããã©ããããšãã£ãé ç®ããã§ãã¯ããããã®æ€èšŒãè¡ãããŸãããªãã·ã§ã³åŒæ° return_annotation ã¯ä»»æã® Python ãªããžã§ã¯ãããšãããšãã§ããŸãããã®åŒæ°ã¯ãåŒã³åºãå¯èœãªããžã§ã¯ãã® "æ»ãå€" ã«å¯Ÿããã¢ãããŒã·ã§ã³ã衚ããŸãã
Signatureãªããžã§ã¯ã㯠immutable ããªãã¡å€æŽäžå¯èœã§ãã倿Žãããã³ããŒãçæããããã«ã¯ãSignature.replace()ãŸãã¯copy.replace()ã䜿ã£ãŠãã ãããããŒãžã§ã³ 3.5 ã§å€æŽ:
Signatureãªããžã§ã¯ã㯠pickle å¯èœã〠ããã·ã¥å¯èœ ã«ãªããŸããã- empty¶
return ã¢ãããŒã·ã§ã³ããªãããšãæãã¯ã©ã¹ã¬ãã«ã®ç¹æ®ããŒã«ã§ãã
- parameters¶
ãã©ã¡ãŒã¿ã®ååãšå¯Ÿå¿ãã
Parameterãªããžã§ã¯ãã®ãé åºãããããã³ã°ã§ãããã©ã¡ãŒã¿ã¯ãããŒã¯ãŒãå°çšãã©ã¡ãŒã¿ãå«ããŠãå³å¯ãªå®çŸ©é ã«çŸããŸããããŒãžã§ã³ 3.7 ã§å€æŽ: ããŒãžã§ã³ 3.7 ãŸã§ã¯ãPython ã¯ããŒã¯ãŒãå°çšãã©ã¡ãŒã¿ã ãããé åºãä¿ã€ããšãæç¢ºã«ä¿èšŒããŠããŸããã§ããããšã¯ãã Python 3 ã§ã¯äºå®äžãã©ã¡ãŒã¿ã®é åºã¯ç¶æãããŠããŸããã
- return_annotation¶
åŒã³åºãå¯èœãªããžã§ã¯ãã® "return" ã¢ãããŒã·ã§ã³ã§ããåŒã³åºãå¯èœãªããžã§ã¯ãã« "return" ã¢ãããŒã·ã§ã³ããªãå Žåããã®å±æ§ã¯
Signature.emptyã«èšå®ãããŸãã
- bind(*args, **kwargs)¶
äœçœ®åŒæ°ããã³ããŒã¯ãŒãåŒæ°ãããã©ã¡ãŒã¿ãžã®ãããã³ã°ãçæããŸãã
*argsãš**kwargsãã·ã°ããã£ã«äžèŽããå ŽåBoundArgumentsãè¿ããŸããåŒæ°ãã·ã°ããã£ãšäžèŽããªãå Žåã¯TypeErroräŸå€ãéåºããŸãã
- bind_partial(*args, **kwargs)¶
Signature.bind()ãšåæ§ã«åäœããŸãããããã€ãã®å¿ èŠãªåŒæ°ãçç¥ããããšãã§ããŸã (functools.partial()ã®æ¯ãèãã«ãã䌌ããã®ã§ã) ãBoundArgumentsãè¿ããŸããåŒæ°ãã·ã°ããã£ãšäžèŽããªãå Žåã¯TypeErroräŸå€ãéåºããŸãã
- replace(*[, parameters][, return_annotation])¶
replace()ã¡ãœãããåŒã³åºãããã€ã³ã¹ã¿ã³ã¹ãããæ°ããSignatureã€ã³ã¹ã¿ã³ã¹ãçæããŸããå ã«ãªãã·ã°ããã£ã®ããããã£ããªãŒããŒã©ã€ãããããã«ç°ãªã parameters ã return_annotation ãæž¡ãããšãå¯èœã§ããreturn_annotationãã³ããŒãããSignatureã€ã³ã¹ã¿ã³ã¹ããåé€ãããå ŽåãSignature.emptyãæž¡ããŠãã ããã>>> def test(a, b): ... pass ... >>> sig = signature(test) >>> new_sig = sig.replace(return_annotation="new return anno") >>> str(new_sig) "(a, b) -> 'new return anno'"
Signatureãªããžã§ã¯ãã®ã³ããŒã¯ãæ±çšã®é¢æ°copy.replace()ã§ããµããŒããããŠããŸãã
- format(*, max_width=None, quote_annotation_strings=True)¶
Signatureãªããžã§ã¯ãã®æåå衚çŸãçæããŸããmax_width ãæå®ããããšãã¡ãœããã¯ã·ã°ããã£ãããããã®è¡ãæå€§ max_width æåã«ãªãããã«ãã©ãŒãããã詊ã¿ãŸããã·ã°ããã£ã max_width ãããé·ããªã£ãŠããŸãå Žåã¯ãå šãŠã®ãã©ã¡ãŒã¿ããšã«æ¹è¡ãããŸãã
quote_annotation_strings ãåœã®å Žåãæååã§è¡šãããã·ã°ããã£ã®äžã® ã¢ãããŒã·ã§ã³ ã¯ãæåãšæåŸã®ã¯ã©ãŒãèšå·ç¡ãã§è¡šç€ºãããŸãããã®æåã¯ãã·ã°ããã£ã
STRINGãã©ãŒãããã§äœãããå Žåãfrom __future__ import annotationsã䜿ãããå Žåã«æçšã§ããAdded in version 3.13.
ããŒãžã§ã³ 3.14 ã§å€æŽ: unquote_annotations ãã©ã¡ãŒã¿ã远å ãããŸããã
- classmethod from_callable(obj, *, follow_wrapped=True, globals=None, locals=None, eval_str=False)¶
äžããããåŒã³åºãå¯èœãªããžã§ã¯ã obj ã«å¯Ÿãã
Signature(ãŸãã¯ãã®ãµãã¯ã©ã¹ã®) ãªããžã§ã¯ããè¿ããŸãããã®ã¡ãœããã¯
Signatureãããµãã¯ã©ã¹ãäœãäœæ¥ãç°¡çŽ åããŸã:class MySignature(Signature): pass sig = MySignature.from_callable(sum) assert isinstance(sig, MySignature)
ãã®ã¡ãœããã®æ¯ãèãã¯ãäžèšãé€ãã°
signature()ãšåãã§ããAdded in version 3.5.
ããŒãžã§ã³ 3.10 ã§å€æŽ: globals, locals, ããã³ eval_str ãã©ã¡ãŒã¿ã远å ãããŸããã
- class inspect.Parameter(name, kind, *, default=Parameter.empty, annotation=Parameter.empty)¶
Parameterãªããžã§ã¯ãã¯ã€ãã¥ãŒã¿ãã« (immutable) ããªãã¡å€æŽäžå¯èœã§ããParameterãªããžã§ã¯ãã倿Žãã代ããã«ãParameter.replace()ãcopy.replace()ã䜿ã£ãŠãªããžã§ã¯ãã®å 容ã倿Žããã³ããŒãäœæããããšãã§ããŸããããŒãžã§ã³ 3.5 ã§å€æŽ: Parameter ãªããžã§ã¯ãã¯ããã¯ã«åå¯èœ (picklable) ã〠ããã·ã¥åå¯èœ (hashable) ã«ãªããŸããã
- empty¶
ããã©ã«ãå€ãšã¢ãããŒã·ã§ã³ããªãããšãæãã¯ã©ã¹ã¬ãã«ã®ç¹æ®ããŒã«ã§ãã
- name¶
ä»®åŒæ°å (æåå) ã§ããååã¯æå¹ãª Python èå¥åã§ãªããã°ãªããŸããã
CPython ã¯å å 衚èšããžã§ãã¬ãŒã¿åŒãå®è£ ããããã®ã³ãŒããªããžã§ã¯ãã«ãããŠã
.0ãšãã圢åŒã§æé»ã®ãã©ã¡ãŒã¿åãçæããŸããããŒãžã§ã³ 3.6 ã§å€æŽ: ãã®ã¢ãžã¥ãŒã«ã«ããããããæé»ã®ãã©ã¡ãŒã¿åã¯
implicit0ã®ãããªååã§å ¬éãããããã«ãªããŸããã
- default¶
åŒæ°ã®ããã©ã«ãå€ã§ããåŒæ°ã«ããã©ã«ãå€ããªãå Žåããã®å±æ§ã¯
Parameter.emptyã«èšå®ãããŸãã
- annotation¶
åŒæ°ã®ã¢ãããŒã·ã§ã³ã§ããåŒæ°ã«ã¢ãããŒã·ã§ã³ããªãå Žåããã®å±æ§ã¯
Parameter.emptyã«èšå®ãããŸãã
- kind¶
åŒæ°ã®å€ãã©ã®ããã«ãã©ã¡ãŒã¿ãšçŽä»ããããããèšè¿°ããŸããæå®å¯èœãªå€ã¯
Parameterãä»ã㊠(Parameter.KEYWORD_ONLYã®ããã«) ã¢ã¯ã»ã¹å¯èœã§ã以äžã«ç€ºãé çªã§æ¯èŒãé åºä»ãããµããŒãããŠããŸã:åå
æå³
POSITIONAL_ONLY
å€ã¯äœçœ®åŒæ°ãšããŠæž¡ãããªããã°ãªããŸããã Python ã®é¢æ°å®çŸ©ã«ãããŠãäœçœ®å°çšåŒæ°ã¯ (ããååšããã°)
/ãšã³ããªã®åã«çŸãããã®ãæããŸããPOSITIONAL_OR_KEYWORD
å€ãããŒã¯ãŒããŸãã¯äœçœ®åŒæ°ãšããŠæž¡ãããšãã§ããŸã (ãã㯠Python ã§å®è£ ããã颿°ã®æšæºçãªæçžåäœã§ã)ã
VAR_POSITIONAL
ãã®ä»ã®ä»®åŒæ°ã«æçžãããŠããªãäœçœ®åŒæ°ã®ã¿ãã«ã§ããPython ã®é¢æ°å®çŸ©ã«ããã
*argsä»®åŒæ°ã«å¯Ÿå¿ããŸããKEYWORD_ONLY
å€ãããŒã¯ãŒãåŒæ°ãšããŠæž¡ããªããã°ãªããŸãããããŒã¯ãŒãå°çšåŒæ°ã¯ Python ã®é¢æ°å®çŸ©ã«ãããŠ
*ã*argsã®åŸã«çŸããåŒæ°ã§ããVAR_KEYWORD
ãã®ä»ã®ä»®åŒæ°ã«æçžãããŠããªãããŒã¯ãŒãåŒæ°ã®èŸæžã§ããPython ã®é¢æ°å®çŸ©ã«ããã
**kwargsä»®åŒæ°ã«å¯Ÿå¿ããŸããäŸ: ããã©ã«ãå€ãæããªãããŒã¯ãŒãå°çšåŒæ°ãå šãŠåºåããŸã:
>>> def foo(a, b, *, c, d=10): ... pass >>> sig = signature(foo) >>> for param in sig.parameters.values(): ... if (param.kind == param.KEYWORD_ONLY and ... param.default is param.empty): ... print('Parameter:', param) Parameter: c
- kind.description¶
Parameter.kindåæåã®å宿°ã説æããŸããAdded in version 3.8.
äŸ: å šãŠã®åŒæ°ã«å¯Ÿãã説æãåºåããŸã:
>>> def foo(a, b, *, c, d=10): ... pass >>> sig = signature(foo) >>> for param in sig.parameters.values(): ... print(param.kind.description) positional or keyword positional or keyword keyword-only keyword-only
- replace(*[, name][, kind][, default][, annotation])¶
ãã®ã¡ãœããã®åŒã³åºãå ã®ã€ã³ã¹ã¿ã³ã¹ãããšã«ããŠãæ°ããª
Parameterã€ã³ã¹ã¿ã³ã¹ãçæããŸããParameterã®å±æ§ããªãŒããŒã©ã€ãããã«ã¯ã察å¿ããåŒæ°ããã®ã¡ãœããã«æž¡ããŠãã ãããParameterããããã©ã«ãå€ãã¢ãããŒã·ã§ã³ãåãé€ãããå Žåã¯ãParameter.emptyãæž¡ããŠãã ããã>>> from inspect import Parameter >>> param = Parameter('foo', Parameter.KEYWORD_ONLY, default=42) >>> str(param) 'foo=42' >>> str(param.replace()) # Will create a shallow copy of 'param' 'foo=42' >>> str(param.replace(default=Parameter.empty, annotation='spam')) "foo: 'spam'"
Parameterãªããžã§ã¯ãã¯æ±çšé¢æ°copy.replace()ã§ããµããŒããããŠããŸãã
ããŒãžã§ã³ 3.4 ã§å€æŽ: Python 3.3 ã§ã¯ã
kindãPOSITIONAL_ONLYã®å Žåã«éããnameãNoneã§ãããããªParameterãªããžã§ã¯ããèš±ãããŠããŸãããã§ããããã®ãããªãªããžã§ã¯ãã¯ãã¯ãèš±å¯ãããŠããŸããã
- class inspect.BoundArguments¶
Signature.bind()ãŸãã¯Signature.bind_partial()ãåŒã³åºããŠåŸãããçµæã§ããåŒæ°ãšé¢æ°ã®ãã©ã¡ãŒã¿ãšã®éã®ãããã³ã°æ å ±ãä¿æããŸãã- arguments¶
ãã©ã¡ãŒã¿ã®ååãšåŒæ°ã®å€ãšã®éã®ãã¥ãŒã¿ãã«ãªãããã³ã°ã§ããæç€ºçã«çŽä»ããããåŒæ°ã ããå«ã¿ãŸãã
argumentsã«å¯Ÿãã倿Žã¯argsãškwargsã«åæ ãããŸãããããªãåŒæ°åŠçã®ç®çã§ãã
Signature.parametersãšããããŠäœ¿ãããã«ããŠãã ãããæ³šé
Signature.bind()ãŸãã¯Signature.bind_partial()ã§ããã©ã«ãå€ãããŠã«ãããšãããåŒæ°ã¯ãã¹ããããããŸãããããããããã³ã°ã«å ãããå Žåã¯ãå¿ èŠã«å¿ããŠBoundArguments.apply_defaults()ã䜿ã£ãŠãã ãããããŒãžã§ã³ 3.9 ã§å€æŽ:
argumentsã®åãdictã«ãªããŸããã以åã¯collections.OrderedDictåã§ããã
- kwargs¶
ããŒã¯ãŒãåŒæ°ã®å€ã®èŸæžã§ãã
arguments屿§ããåçã«èšç®ãããŸããäœçœ®åŒæ°ãšããŠæž¡ãããšãã§ããåŒæ°ã¯ãargsã«å«ãŸããããšã«æ³šæããŠãã ããã
- apply_defaults()¶
ååšããªãåŒæ°ã®ããã©ã«ãå€ãèšå®ããŸãã
å¯å€é·äœçœ®åŒæ° (
*args) ã«å¯Ÿããããã©ã«ãå€ã¯ã空ã®ã¿ãã«ã§ããå¯å€é·ããŒã¯ãŒãåŒæ° (
**kwargs) ã«å¯Ÿããããã©ã«ãå€ã¯ã空ã®èŸæžã§ãã>>> def foo(a, b='ham', *args): pass >>> ba = inspect.signature(foo).bind('spam') >>> ba.apply_defaults() >>> ba.arguments {'a': 'spam', 'b': 'ham', 'args': ()}
Added in version 3.5.
argsãškwargsã®2ã€ã®ããããã£ã¯ã颿°ã®åŒã³åºãã«äœ¿ãããšãã§ããŸã:def test(a, *, b): ... sig = signature(test) ba = sig.bind(10, b=20) test(*ba.args, **ba.kwargs)
åè
- PEP 362: - 颿°ã·ã°ããã£ãªããžã§ã¯ã
仿§ã®è©³çްãå®è£ ã®è©³çްããã³äœ¿çšäŸã瀺ããŠããŸãã
ã¯ã©ã¹ãšé¢æ°Â¶
- inspect.getclasstree(classes, unique=False)¶
ãªã¹ãã§æå®ããã¯ã©ã¹ã®ç¶æ¿é¢ä¿ããããã¹ããããªã¹ããäœæããŸãããã¹ããããªã¹ãã«ã¯ãçŽåã®èŠçŽ ããæŽŸçããã¯ã©ã¹ãæ ŒçŽãããŸããåèŠçŽ ã¯é·ã2ã®ã¿ãã«ã§ãã¯ã©ã¹ãšåºåºã¯ã©ã¹ã®ã¿ãã«ãæ ŒçŽããŠããŸããunique ãçã®å Žåãåã¯ã©ã¹ã¯æ»ãå€ã®ãªã¹ãå ã«äžã€ã ãããæ ŒçŽãããŸãããçã§ãªããã°ãå€éç¶æ¿ãå©çšããã¯ã©ã¹ãšãã®æŽŸçã¯ã©ã¹ã¯è€æ°åæ ŒçŽãããå ŽåããããŸãã
- inspect.getfullargspec(func)¶
Python 颿°ã®ãã©ã¡ãŒã¿ã®ååãšããã©ã«ãå€ãååŸããŸãã named tuple ãè¿ããŸã:
FullArgSpec(args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations)args ã¯äœçœ®åŒæ°ã®ååã®ãªã¹ãã§ãã varargs ã¯
*ã§å§ãŸãå¯å€é·äœçœ®åŒæ°ã®ååã§ããã®åŒæ°ãåãä»ããªãå Žåã¯Noneã§ãã varkw ã¯**ã§å§ãŸãå¯å€é·ããŒã¯ãŒãåŒæ°ã®ååã§ããã®åŒæ°ãåãä»ããªãå Žåã¯Noneã§ãã defaults ã¯æ«å°Ÿ n åã®äœçœ®åŒæ°ã«å¯Ÿå¿ããããã©ã«ãåŒæ°ã® n-ã¿ãã«ã§ã ãã®ãããªããã©ã«ãå€ãå®çŸ©ãããŠããªãå Žåã¯Noneã§ãã kwonlyargs ã¯å®£èšé ã«äžŠãã ããŒã¯ãŒãå°çšåŒæ°ã®ååã®ãªã¹ãã§ãã kwonlydefaults ã¯ãããã«å¯ŸããŠå€ãæž¡ãããªãã£ãå Žåã®ã kwonlyargs ã®ååããããã©ã«ãå€ãžã®ãããã³ã°ãè¡ãèŸæžã§ãã annotations åŒæ°åããã¢ãããŒã·ã§ã³ãžã®ãããã³ã°ãè¡ãèŸæžã§ããç¹æ®ããŒ"return"㯠(ããããã°) 颿°ã®æ»ãå€ã«å¯Ÿããã¢ãããŒã·ã§ã³ãäŒããããã«äœ¿ãããŸããsignature()ãš Signature ãªããžã§ã¯ã ãåŒã³åºãå¯èœãªããžã§ã¯ãã®ã€ã³ããã¹ãã¯ã·ã§ã³ (å®è¡æãªããžã§ã¯ã調æ») ã®ããã®æšå¥šããã API ãæäŸããŠããããã€æ¡åŒµã¢ãžã¥ãŒã« API ã§ãšãã©ãééãã远å ã®æ¯ãèã (äœçœ®å°çšåŒæ°ãªã©) ããµããŒãããŠããããšã«æ³šæããŠãã ããããã®é¢æ°ã¯ã䞻㫠Python 2 ã®inspectã¢ãžã¥ãŒã« API ãšã®äºææ§ãç¶æããããšãå¿ èŠãªã³ãŒãã§å©çšãããããã«æ®ãããŠããŸããããŒãžã§ã³ 3.4 ã§å€æŽ: ãã®é¢æ°ã¯
signature()ãããšã«å®è£ ãããŠããŸãããã ãã__wrapped__屿§ãç¡èŠããŸãããŸããæçžãããã¡ãœããã«å¯Ÿããã·ã°ããã£ã®åºåã«ãããŠããã§ã«æçžãããæåã®ãã©ã¡ãŒã¿ãå«ã¿ãŸããããŒãžã§ã³ 3.6 ã§å€æŽ: This method was previously documented as deprecated in favour of
signature()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 legacygetargspec()API.ããŒãžã§ã³ 3.7 ã§å€æŽ: ããŒãžã§ã³ 3.7 ãŸã§ã¯ãPython ã¯ããŒã¯ãŒãå°çšãã©ã¡ãŒã¿ã ãããé åºãä¿ã€ããšãæç¢ºã«ä¿èšŒããŠããŸããã§ããããšã¯ãã Python 3 ã§ã¯äºå®äžãã©ã¡ãŒã¿ã®é åºã¯ç¶æãããŠããŸããã
- inspect.getargvalues(frame)¶
æå®ãããã¬ãŒã ã«æž¡ãããåŒæ°ã®æ å ±ãååŸããŸããæ»ãå€ã¯ ååä»ãã¿ãã«
ArgInfo(args, varargs, keywords, locals)ã§ããargs ã¯åŒæ°åã®ãªã¹ãã§ãã varargs ãš keywords ã¯*åŒæ°ãš**åŒæ°ã®ååã§ãåŒæ°ããªããã°NoneãšãªããŸãã locals ã¯æå®ãããã¬ãŒã ã®ããŒã«ã«å€æ°ã®èŸæžã§ããæ³šé
ãã®é¢æ°ã¯ Python 3.5 ã«ãããŠãäžæ³šæã«ããéæšå¥šãšãããŠããŸããã
- inspect.formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue])¶
getargvalues()ã§ååŸãã4ã€ã®å€ãèªã¿ãããæŽåœ¢ããŸãã format* åŒæ°ã¯ãªãã·ã§ã³ã§ãååãšå€ãæååã«å€æããæŽåœ¢é¢æ°ãæå®ããããšãã§ããŸããæ³šé
ãã®é¢æ°ã¯ Python 3.5 ã«ãããŠãäžæ³šæã«ããéæšå¥šãšãããŠããŸããã
- inspect.getmro(cls)¶
cls ã¯ã©ã¹ã®åºåºã¯ã©ã¹ (cls èªèº«ãå«ã) ããã¡ãœããã®åªå é äœé ã«äžŠã¹ãã¿ãã«ãè¿ããŸããçµæã®ãªã¹ãå ã§åã¯ã©ã¹ã¯äžåºŠã ãæ ŒçŽãããŸããã¡ãœããã®åªå é äœã¯ã¯ã©ã¹ã®åã«ãã£ãŠç°ãªããŸããéåžžã«ç¹æ®ãªãŠãŒã¶å®çŸ©ã®ã¡ã¿ã¯ã©ã¹ã䜿çšããŠããªãéããcls ãæ»ãå€ã®å é èŠçŽ ãšãªããŸãã
- inspect.getcallargs(func, /, *args, **kwds)¶
args ãš kwds ãããããããããããã©ã¡ãŒã¿ãšããŠåŒã°ãããã®ããã«ã Python 颿°ãŸãã¯ã¡ãœãã func ã«æçžããŸãããŸãã第äžåŒæ° (éåžž
selfãšããååã§ã) ãé¢é£ããã€ã³ã¹ã¿ã³ã¹ã«æçžããŸããåŒæ°å (ããååšããã°*ã**ã§å§ãŸãåŒæ°ãå«ã) ã args ããã³ kwds ã§äžããããå€ã«ãããã³ã°ããèŸæžãè¿ããŸãã func ãäžæ£ã«åŒã³åºããå Žåãããªãã¡func(*args, **kwds)ã®åŒã³åºããäžå®å šãªã·ã°ããã£ã«ããäŸå€ãéåºãããããªå Žåã¯ãåãåã®äŸå€ãåäžãŸãã¯ãã䌌ãã¡ãã»ãŒãžãšãšãã«éåºããŸãã以äžã¯äœ¿çšäŸã§ã:>>> from inspect import getcallargs >>> def f(a, b=1, *pos, **named): ... pass ... >>> getcallargs(f, 1, 2, 3) == {'a': 1, 'named': {}, 'b': 2, 'pos': (3,)} True >>> getcallargs(f, a=2, x=4) == {'a': 2, 'named': {'x': 4}, 'b': 1, 'pos': ()} True >>> getcallargs(f) Traceback (most recent call last): ... TypeError: f() missing 1 required positional argument: 'a'
Added in version 3.2.
ããŒãžã§ã³ 3.5 ã§éæšå¥š: 代ããã«
Signature.bind()ãSignature.bind_partial()ã䜿ã£ãŠãã ããã
- inspect.getclosurevars(func)¶
Python 颿°ãŸãã¯ã¡ãœãã func ã®å€éšã®åååç §ãšããããã®çŸåšã®å€ã®ãããã³ã°ãååŸããŸãã named tuple
ClosureVars(nonlocals, globals, builtins, unbound)ãè¿ããŸããåç §åãã nonlocals ã¯ã¬ãã·ã«ã«ã¯ããŒãžã£å€æ°ãããªãã¡å€åŽã®ã¹ã³ãŒãã§å®çŸ©ããã倿°ã®å€ã«ããããã globals ã¯é¢æ°ãå±ããã¢ãžã¥ãŒã«ã®ã°ããŒãã«å€æ°ã«ããããã builtins ã¯é¢æ°æ¬äœããèŠããçµã¿èŸŒã¿å€æ°ã«ãããããŸãã unbound ã¯ãçŸåšã®ã¢ãžã¥ãŒã«ã«ãããã°ããŒãã«å€æ°ãçµã¿èŸŒã¿å€æ°ã«ãããŠè§£æ±ºã§ããªãã«ããããããã颿°å ã§åç §ãããŠããååã®éåã§ããfunc ã Python ã®é¢æ°ãã¡ãœããã§ãªãå Žå
TypeErrorãéåºãããŸããAdded in version 3.3.
- inspect.unwrap(func, *, stop=None)¶
func ã«ããã©ããããããªããžã§ã¯ããååŸããŸãã
__wrapped__屿§ã®é£éããã©ããã©ããã®é£éã®æåŸã«ãããªããžã§ã¯ããè¿ããŸããstop ã¯ãªãã·ã§ã³ã§æå®å¯èœãªã³ãŒã«ããã¯é¢æ°ã§ãã©ãããŒã®é£éã«ããããªããžã§ã¯ãããã ã²ãšã€ã®åŒæ°ãšããŠåããŸãããã®ã³ãŒã«ããã¯é¢æ°ãçå€ãè¿ããæ®µéã§ããªããžã§ã¯ãã®ã¢ã³ã©ãããæ©æã«çµäºãããããšãã§ããŸããã³ãŒã«ããã¯é¢æ°ãçå€ãè¿ããªãå Žåã¯ãéåžžéãã©ãããŒã®é£éã«ãããæåŸã®ãªããžã§ã¯ããè¿ããŸããäŸãã°ã
signature()ã¯ããªããžã§ã¯ãã«__signature__屿§ãå®çŸ©ãããŠããããªããžã§ã¯ãã®ã¢ã³ã©ããã忢ããããã«ããã®ãªãã·ã§ã³ã䜿ã£ãŠããŸãã埪ç°ãæ€ç¥ããå Žåã¯
ValueErrorãéåºããŸããAdded in version 3.4.
- inspect.get_annotations(obj, *, globals=None, locals=None, eval_str=False, format=annotationlib.Format.VALUE)¶
ãªããžã§ã¯ãã«å¯Ÿããã¢ãããŒã·ã§ã³èŸæžãèšç®ããŸãã
ããã¯
annotationlib.get_annotations()ã®ãšã€ãªã¢ã¹ã§ã; 詳现ã¯ãã®é¢æ°ã®ããã¥ã¡ã³ããåç §ããŠãã ãããæ³šæ
This function may execute arbitrary code contained in annotations. See Security implications of introspecting annotations for more information.
Added in version 3.10.
ããŒãžã§ã³ 3.14 ã§å€æŽ: ãã®é¢æ°ã¯
annotationlib.get_annotations()ã®ãšã€ãªã¢ã¹ã«ãªããŸãããinspect.get_annotationsãšããŠåŒã³åºããŠããåŒãç¶ãæ£ããåäœããŸãã
ã€ã³ã¿ãŒããªã¿ã¹ã¿ãã¯Â¶
以äžã®é¢æ°ã®ãã¡ããã€ã㯠FrameInfo ãªããžã§ã¯ããè¿ããŸãããããã®ãªããžã§ã¯ãã¯ãåŸæ¹äºææ§ã®ããã« positions ãé€ãå
šãŠã®å±æ§ã«å¯ŸããŠãã¿ãã«çãªæŒç®ãèš±å¯ããŸãããã®æ¯ãèãã¯éæšå¥šãšèããããŠãããå°æ¥åé€ããããããããŸããã
- class inspect.FrameInfo¶
- frame¶
ãã®ã¬ã³ãŒãã«çžåœãã ãã¬ãŒã ãªããžã§ã¯ã ã§ãã
- filename¶
ãã®ã¬ã³ãŒãã«çžåœãããã¬ãŒã ã«ãã£ãŠå®è¡ãããã³ãŒãã®ãã¡ã€ã«åã§ãã
- lineno¶
ãã®ã¬ã³ãŒãã«çžåœãããã¬ãŒã ã«ãã£ãŠå®è¡ãããã³ãŒãã®ãçŸåšã®è¡ã®è¡çªå·ã§ãã
- function¶
ãã®ã¬ã³ãŒãã«çžåœãããã¬ãŒã ã«ãã£ãŠå®è¡ããã颿°åã§ãã
- code_context¶
ãã®ã¬ã³ãŒãã«çžåœãããã¬ãŒã ã«ãã£ãŠå®è¡ãããããœãŒã¹ã³ãŒãããçæãããã³ã³ããã¹ãã®è¡ã®ãªã¹ãã§ãã
- index¶
code_contextãªã¹ãã«ãããŠå®è¡ãããçŸåšã®è¡ã®ã€ã³ããã¯ã¹ã§ãã
- positions¶
ãã®ã¬ã³ãŒãã«çžåœãããã¬ãŒã ã«ãã£ãŠå®è¡ãããåœä»€ã«çµã³ã€ãã
dis.Positionsãªããžã§ã¯ãã§ãéå§è¡çªå·ãçµäºè¡çªå·ãéå§ã«ã©ã ãªãã»ãããçµäºã«ã©ã ãªãã»ãããå«ãã§ããŸãã
ããŒãžã§ã³ 3.5 ã§å€æŽ:
tupleã§ã¯ãªã named tuple ãè¿ãããã«ãªããŸãããããŒãžã§ã³ 3.11 ã§å€æŽ:
FrameInfoã¯ã¯ã©ã¹ã€ã³ã¹ã¿ã³ã¹ã«ãªããŸãã (ãã ããã以åã® named tuple ãšã¯åŸæ¹äºæã§ã) ã
- class inspect.Traceback¶
- filename¶
ãã®ãã¬ãŒã¹ããã¯ã«å¯Ÿå¿ãããã¬ãŒã ã«ãã£ãŠå®è¡ãããã³ãŒãã®ãã¡ã€ã«åã§ãã
- lineno¶
ãã®ãã¬ãŒã¹ããã¯ã«å¯Ÿå¿ãããã¬ãŒã ã«ãã£ãŠå®è¡ãããã³ãŒãã®ãçŸåšã®è¡ã®è¡çªå·ã§ãã
- function¶
ãã®ãã¬ãŒã¹ããã¯ã«å¯Ÿå¿ãããã¬ãŒã ã«ãã£ãŠå®è¡ããã颿°åã§ãã
- code_context¶
ãã®ãã¬ãŒã¹ããã¯ã«å¯Ÿå¿ãããã¬ãŒã ã«ãã£ãŠå®è¡ãããããœãŒã¹ã³ãŒãããçæãããã³ã³ããã¹ãã®è¡ã®ãªã¹ãã§ãã
- index¶
code_contextãªã¹ãã«ãããŠå®è¡ãããçŸåšã®è¡ã®ã€ã³ããã¯ã¹ã§ãã
- positions¶
ãã®ãã¬ãŒã¹ããã¯ã«å¯Ÿå¿ãããã¬ãŒã ã«ãã£ãŠå®è¡ãããåœä»€ã®éå§è¡çªå·ãçµäºè¡çªå·ãéå§ã«ã©ã ãªãã»ãããçµäºã«ã©ã ãªãã»ãããå«ã
dis.Positionsãªããžã§ã¯ãã§ãã
ããŒãžã§ã³ 3.11 ã§å€æŽ:
Tracebackã¯ã¯ã©ã¹ã€ã³ã¹ã¿ã³ã¹ã«ãªããŸãã (ãã ããã以åã® named tuple ãšã¯åŸæ¹äºæã§ã) ã
泚é
ãã¬ãŒã ã¬ã³ãŒãã®æåã®èŠçŽ ãªã©ã®ãã¬ãŒã ãªããžã§ã¯ããžã®åç §ãä¿åãããšã埪ç°åç §ã«ãªã£ãŠããŸãå ŽåããããŸãã埪ç°åç §ãã§ãããšãPython ã®åŸªç°åç §æ€åºæ©èœãæå¹ã«ããŠãããšããŠãé¢é£ãããªããžã§ã¯ããåç §ããŠãããã¹ãŠã®ãªããžã§ã¯ããè§£æŸããã«ãããªããæç€ºçã«åç §ãåé€ããªããšã¡ã¢ãªæ¶è²»éãå¢å€§ããæãããããŸãã
åç
§ã®åé€ã Python ã®åŸªç°åç
§æ€åºæ©èœã«ãŸãããããšãã§ããŸããã finally ç¯ã§åŸªç°åç
§ãè§£é€ããã°ç¢ºå®ã«ãã¬ãŒã (ãšãã®ããŒã«ã«å€æ°) ã¯åé€ãããŸãããŸãã埪ç°åç
§æ€åºæ©èœã¯ Python ã®ã³ã³ãã€ã«ãªãã·ã§ã³ã gc.disable() ã§ç¡å¹ãšãããŠããå ŽåããããŸãã®ã§æ³šæãå¿
èŠã§ããäŸ:
def handle_stackframe_without_leak():
frame = inspect.currentframe()
try:
# ãã¬ãŒã äžã§äœãåŠçãããŸã
finally:
del frame
(ããšãã°ãã¬ãŒã¹ããã¯ãããšã§è¡šç€ºããããªã©ã®çç±ã§) ãã¬ãŒã ãåé€ããã«ãšã£ãŠããããå Žåã¯ã frame.clear() ã¡ãœããã䜿ã£ãŠåŸªç°åç
§ãå£ãããšãã§ããŸãã
以äžã®é¢æ°ã§ãªãã·ã§ã³åŒæ° context ã«ã¯ãæ»ãå€ã®ãœãŒã¹è¡ãªã¹ãã«äœè¡åã®ãœãŒã¹ãå«ããããæå®ããŸãããœãŒã¹è¡ãªã¹ãã«ã¯ãå®è¡äžã®è¡ãäžå¿ãšããŠæå®ãããè¡æ°åã®ãªã¹ããè¿ããŸãã
- inspect.getframeinfo(frame, context=1)¶
ãã¬ãŒã ãŸãã¯ãã¬ãŒã¹ããã¯ãªããžã§ã¯ãã®æ å ±ãååŸããŸãã
Tracebackãªããžã§ã¯ããè¿ãããŸããããŒãžã§ã³ 3.11 ã§å€æŽ: ååä»ãã¿ãã«ã®ä»£ããã«
Tracebackãªããžã§ã¯ããè¿ãããããã«ãªããŸããã
- inspect.getouterframes(frame, context=1)¶
æå®ãããã¬ãŒã ãšããã®å€åŽã®å šãã¬ãŒã ã®
FrameInfoãªããžã§ã¯ãã®ãªã¹ããååŸããŸããå€åŽã®ãã¬ãŒã ãšã¯ frame ãçæããããŸã§ã®ãã¹ãŠã®é¢æ°åŒã³åºãã瀺ããŸããæ»ãå€ã®ãªã¹ãã®å é 㯠frame ã®ãã¬ãŒã ã¬ã³ãŒãã§ãæ«å°Ÿã®èŠçŽ ã¯ frame ã®ã¹ã¿ãã¯ã«ããæãå€åŽã®ãã¬ãŒã ã®ãã¬ãŒã ã¬ã³ãŒããšãªããŸããããŒãžã§ã³ 3.5 ã§å€æŽ: ååä»ãã¿ãã«
FrameInfo(frame, filename, lineno, function, code_context, index)ã®ãªã¹ããè¿ãããŸããããŒãžã§ã³ 3.11 ã§å€æŽ:
FrameInfoãªããžã§ã¯ãã®ãªã¹ããè¿ãããããã«ãªããŸããã
- inspect.getinnerframes(traceback, context=1)¶
ãã¬ãŒã¹ããã¯ã®ãã¬ãŒã ãšããã®å åŽã®å šãã¬ãŒã ã®
FrameInfoãªããžã§ã¯ãã®ãªã¹ããååŸããŸããå ã®ãã¬ãŒã ãšã¯ frame ããç¶ãäžé£ã®é¢æ°åŒã³åºãã瀺ããŸããæ»ãå€ã®ãªã¹ãã®å é 㯠traceback ã®ãã¬ãŒã ã¬ã³ãŒãã§ãæ«å°Ÿã®èŠçŽ ã¯äŸå€ãçºçããäœçœ®ã瀺ããŸããããŒãžã§ã³ 3.5 ã§å€æŽ: ååä»ãã¿ãã«
FrameInfo(frame, filename, lineno, function, code_context, index)ã®ãªã¹ããè¿ãããŸããããŒãžã§ã³ 3.11 ã§å€æŽ:
FrameInfoãªããžã§ã¯ãã®ãªã¹ããè¿ãããããã«ãªããŸããã
- inspect.currentframe()¶
åŒã³åºãå ã®ãã¬ãŒã ãªããžã§ã¯ããè¿ããŸãã
ãã®é¢æ°ã¯ã€ã³ã¿ããªã¿ã® Python ã¹ã¿ãã¯ãã¬ãŒã ãµããŒãã«äŸåããŸãããã㯠Python ã®ãã¹ãŠã®å®è£ ã«ååšããŠããä¿èšŒã¯ãããŸãããPython ã¹ã¿ãã¯ãã¬ãŒã ãµããŒãã®ãªãç°å¢ã§ã¯ããã®é¢æ°ã¯
Noneãè¿ããŸãã
- inspect.stack(context=1)¶
åŒã³åºãå ã¹ã¿ãã¯ã®
FrameInfoãªããžã§ã¯ãã®ãªã¹ããè¿ããŸããæåã®èŠçŽ ã¯åŒã³åºãå ã®ãã¬ãŒã ã¬ã³ãŒãã§ãæ«å°Ÿã®èŠçŽ ã¯ã¹ã¿ãã¯ã«ããæãå€åŽã®ãã¬ãŒã ã®ãã¬ãŒã ã¬ã³ãŒããšãªããŸããããŒãžã§ã³ 3.5 ã§å€æŽ: ååä»ãã¿ãã«
FrameInfo(frame, filename, lineno, function, code_context, index)ã®ãªã¹ããè¿ãããŸããããŒãžã§ã³ 3.11 ã§å€æŽ:
FrameInfoãªããžã§ã¯ãã®ãªã¹ããè¿ãããããã«ãªããŸããã
- inspect.trace(context=1)¶
å®è¡äžã®ãã¬ãŒã ãšåŠçäžã®äŸå€ãçºçãããã¬ãŒã ã®éã®
FrameInfoãªããžã§ã¯ãã®ãªã¹ããè¿ããŸããæåã®èŠçŽ ã¯åŒã³åºãå ã®ãã¬ãŒã ã¬ã³ãŒãã§ãæ«å°Ÿã®èŠçŽ ã¯äŸå€ãçºçããäœçœ®ã瀺ããŸããããŒãžã§ã³ 3.5 ã§å€æŽ: ååä»ãã¿ãã«
FrameInfo(frame, filename, lineno, function, code_context, index)ã®ãªã¹ããè¿ãããŸããããŒãžã§ã³ 3.11 ã§å€æŽ:
FrameInfoãªããžã§ã¯ãã®ãªã¹ããè¿ãããããã«ãªããŸããã
屿§ã®éçãªãã§ãã¶
getattr() ã hasattr() ã¯å±æ§ã®ååšç¢ºèªã屿§å€ã®ååŸã«éããŠã³ãŒãã®å®è¡ã䌎ãããšããããŸããããããã£ãªã©ã®ãã¹ã¯ãªãã¿ãåŒã³åºããããŸã __getattr__() ã __getattribute__() ãåŒã³åºãããããããããããŸããã
For cases where you want passive introspection, like documentation tools, this
can be inconvenient. getattr_static() has a similar signature as getattr()
but avoids executing code when it fetches attributes.
- inspect.getattr_static(obj, attr)¶
- inspect.getattr_static(obj, attr, default)
Retrieve attributes without triggering dynamic lookup via the descriptor protocol,
__getattr__()or__getattribute__().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.
If the instance
__dict__is shadowed by another member (for example a property) then this function will be unable to find instance members.Added in version 3.2.
getattr_static() 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.
You can handle these with code like the following. Note that for arbitrary getset descriptors invoking these may trigger code execution:
# example code for resolving the builtin descriptor types
class _foo:
__slots__ = ['foo']
slot_descriptor = type(_foo.foo)
getset_descriptor = type(type(open(__file__)).name)
wrapper_descriptor = type(str.__dict__['__add__'])
descriptor_types = (slot_descriptor, getset_descriptor, wrapper_descriptor)
result = getattr_static(some_object, 'foo')
if type(result) in descriptor_types:
try:
result = result.__get__()
except AttributeError:
# descriptors can raise AttributeError to
# indicate there is no underlying value
# in which case the descriptor itself will
# have to do
pass
Current State of Generators, Coroutines, and Asynchronous Generators¶
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. getgeneratorstate() allows the current state of a
generator to be determined easily.
- inspect.getgeneratorstate(generator)¶
ãžã§ãã¬ãŒã¿ã€ãã¬ãŒã¿ã®çŸåšã®ç¶æ ãååŸããŸãã
åãåŸãç¶æ ã¯:
GEN_CREATED: å®è¡éå§ãåŸ æ©ããŠããŸãã
GEN_RUNNING: ã€ã³ã¿ãŒããªã¿ã«ãã£ãŠçŸåšå®è¡ãããŠããŸãã
GEN_SUSPENDED: yield åŒã§çŸåšãµã¹ãã³ããããŠããŸãã
GEN_CLOSED: å®è¡ãå®äºããŸããã
Added in version 3.2.
- inspect.getcoroutinestate(coroutine)¶
Get current state of a coroutine object. The function is intended to be used with coroutine objects created by
async deffunctions, but will accept any coroutine-like object that hascr_runningandcr_frameattributes.åãåŸãç¶æ ã¯:
CORO_CREATED: å®è¡éå§ãåŸ æ©ããŠããŸãã
CORO_RUNNING: ã€ã³ã¿ãŒããªã¿ã«ããçŸåšå®è¡äžã§ãã
CORO_SUSPENDED: await åŒã«ããçŸåšåæ¢äžã§ãã
CORO_CLOSED: å®è¡ãå®äºããŸããã
Added in version 3.5.
- inspect.getasyncgenstate(agen)¶
Get current state of an asynchronous generator object. The function is intended to be used with asynchronous iterator objects created by
async deffunctions which use theyieldstatement, but will accept any asynchronous generator-like object that hasag_runningandag_frameattributes.åãåŸãç¶æ ã¯:
AGEN_CREATED: Waiting to start execution.
AGEN_RUNNING: Currently being executed by the interpreter.
AGEN_SUSPENDED: Currently suspended at a yield expression.
AGEN_CLOSED: Execution has completed.
Added in version 3.12.
ãžã§ãã¬ãŒã¿ã®çŸåšã®å éšç¶æ ãåãåãããããšãåºæ¥ãŸããããã¯äž»ã«å éšç¶æ ãæåŸ éãæŽæ°ãããŠãããã©ããã確èªããããã®ãã¹ãã®ç®çã«æçšã§ãã
- inspect.getgeneratorlocals(generator)¶
Get the mapping of live local variables in generator to their current values. A dictionary is returned that maps from variable names to values. This is the equivalent of calling
locals()in the body of the generator, and all the same caveats apply.If generator is a generator with no currently associated frame, then an empty dictionary is returned.
TypeErroris raised if generator is not a Python generator object.CPython å®è£ ã®è©³çް: 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.
Added in version 3.3.
- inspect.getcoroutinelocals(coroutine)¶
This function is analogous to
getgeneratorlocals(), but works for coroutine objects created byasync deffunctions.Added in version 3.5.
- inspect.getasyncgenlocals(agen)¶
This function is analogous to
getgeneratorlocals(), but works for asynchronous generator objects created byasync deffunctions which use theyieldstatement.Added in version 3.12.
Code Objects Bit Flags¶
Python code objects have a co_flags attribute,
which is a bitmap of the following flags:
- inspect.CO_OPTIMIZED¶
The code object is optimized, using fast locals.
- inspect.CO_NEWLOCALS¶
If set, a new dict will be created for the frame's
f_localswhen the code object is executed.
- inspect.CO_VARARGS¶
The code object has a variable positional parameter (
*args-like).
- inspect.CO_VARKEYWORDS¶
The code object has a variable keyword parameter (
**kwargs-like).
- inspect.CO_NESTED¶
The flag is set when the code object is a nested function.
- inspect.CO_GENERATOR¶
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.
- inspect.CO_COROUTINE¶
The flag is set when the code object is a coroutine function. When the code object is executed it returns a coroutine object. See PEP 492 for more details.
Added in version 3.5.
- inspect.CO_ITERABLE_COROUTINE¶
The flag is used to transform generators into generator-based coroutines. Generator objects with this flag can be used in
awaitexpression, and canyield fromcoroutine objects. See PEP 492 for more details.Added in version 3.5.
- inspect.CO_ASYNC_GENERATOR¶
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 PEP 525 for more details.
Added in version 3.6.
- inspect.CO_HAS_DOCSTRING¶
The flag is set when there is a docstring for the code object in the source code. If set, it will be the first item in
co_consts.Added in version 3.14.
- inspect.CO_METHOD¶
The flag is set when the code object is a function defined in class scope.
Added in version 3.14.
泚é
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 inspect module
for any introspection needs.
Buffer flags¶
- class inspect.BufferFlags¶
This is an
enum.IntFlagthat represents the flags that can be passed to the__buffer__()method of objects implementing the buffer protocol.The meaning of the flags is explained at ãããã¡ãªã¯ãšã¹ãã®ã¿ã€ã.
- SIMPLE¶
- WRITABLE¶
- FORMAT¶
- ND¶
- STRIDES¶
- C_CONTIGUOUS¶
- F_CONTIGUOUS¶
- ANY_CONTIGUOUS¶
- INDIRECT¶
- CONTIG¶
- CONTIG_RO¶
- STRIDED¶
- STRIDED_RO¶
- RECORDS¶
- RECORDS_RO¶
- FULL¶
- FULL_RO¶
- READ¶
- WRITE¶
Added in version 3.12.
ã³ãã³ãã©ã€ã³ã»ã€ã³ã¿ãŒãã§ãŒã¹Â¶
The inspect module also provides a basic introspection capability
from the command line.
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.
- --details¶
Print information about the specified object rather than the source code