Title: Theory / Unimportant / CS / Programming / Python / Lib / Inspect
Tags: FromInstruction computer_to_instruction automatedNewLineAfterCommaSpace FromVariables
On every note of this deck, unless a note says otherwise:
Language: Python

Instruction: getmembers(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: all the members of the object foo
Typ: list of (name, value)

Instruction: getmembers(
  foo,
  bar
)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: all the members m of the object foo satisfying bar(  m)
Typ: list of (
  name,
  value
)

Instruction: getmodulename(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: the name of the module named by the file foo, without including the names of enclosing packages.
Assuming: foo an actual python module
Typ: meaningful name

Instruction: ismodule(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: whether foo is a module

Instruction: isclass(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: whether foo is a class

Instruction: ismethod(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: whether foo is a bound method

Instruction: isfunction(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: whether foo is a function (even a lambda expression)

Instruction: isgeneratorfunction(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: whether foo is a Python generator function

Instruction: isgenerator(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: whether foo is a Python generator

Instruction: iscoroutinefunction(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: whether foo is a coroutine function (defined by async def)

Instruction: iscoroutine(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: whether foo is a coroutine (i.e. defined by async def)

Instruction: isasyncgenfunction(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: Whether foo is an asynchronous generator function

Instruction: isasyncgen(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: Whether foo is an asynchronous generator iterator

Instruction: istraceback(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: Whether foo is a traceback

Instruction: isframe(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: Whether foo is a frame

Instruction: iscode(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: Whether foo is code

Instruction: isbuiltin(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: Whether foo is a builtin function/method

Instruction: isabstract(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: Whether foo is an abstract base class

Instruction: ismethoddescriptor(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: Whether its a method description, not ismethod, isclass, isfunction or isbuiltin

Instruction: isdatadescriptor(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: Whether foo is a data descriptor.

Instruction: isgetsetdescriptor(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: Whether foo is a getset descriptor.

Instruction: ismemberdescriptor(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: Whether foo is a member descriptor.

Instruction: getdoc(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: the documentation string for foo,
Extra: cleaned up with cleandoc()

Instruction: getcomments(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: a single string any lines of comments immediately preceding foo's source code (for a class, function, or method), or at the top of the Python source file (if the object is a module).

Instruction: getfile(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: the name of the (text or binary) file in which an object was defined

Instruction: getmodule(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: A guess of which module an object was defined in.

Instruction: getsourcefile(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: the name of the Python source file in which an object was defined.

Instruction: getsourcelines(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: a list of source lines and starting line number for foo

Instruction: getsource(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: The text of the source code foo

Instruction: cleandoc(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: The docstring foo cleaned up. (Unindenting lines)

Instruction: signature(foo)
Page: https://docs.python.org/3/library/inspect.html
Context: Inspect
Returns: Signature of foo

Variable: Signature.empty
Meaning: A special class-level marker to specify absence of a return annotation.
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Language: Python

Variable: s.parameters
Meaning: ordered mapping of parameters’ names to the corresponding Parameter objects
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Language: Python
Variables: s:Signature

Instruction: s.bind(*args,**kwargs)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Returns: Create a mapping from positional and keyword arguments to parameters. Returns BoundArguments if *args and **kwargs match the signature, or raises a TypeError.
Variables: s:Signature
Exception: TypeError

Instruction: s.bind_partial(*args,**kwargs)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Returns: Works the same way as s.bind(*args,**kwargs), but allows the omission of some required arguments (mimics functools.partial() behavior.) Returns BoundArguments, or raises a TypeError if the passed arguments do not match the signature.
Variables: s:Signature

Instruction: s.replace(
  *[,
  parameters][,
  return_annotation]
)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Returns: a new Signature instance based on the instance replace was invoked on.
Variables: s:Signature
Extra: It is possible to pass different parameters and/or return_annotation to override the corresponding properties of the base signature. To remove return_annotation from the copied Signature,
  pass in Signature.empty.

Instruction: Signature.from_callable(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect.
Returns: a Signature (or its subclass) object for a callable foo
Variables: s:Signature

Instruction: Signature.from_callable(
  foo,
  follow_wrapped=False
)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect.
Returns: a Signature (  or its subclass) object for a callable foo. Without unwrapping its __wrapped__chain

Instruction: p.replace(foo_i=bar_i)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Effect: a new Parameter instance based on the instance replaced was invoked on, with foo_i based on bar_i
Context: inspect
Variables: p:Parameter

Instruction: b.apply_defaults()
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Effect: Set default values for missing arguments.
Context: inspect
Variables: b:BoundArguments

Instruction: getclasstree(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Effect: Arrange the list of classes foo 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. Classes using multiple inheritance and their descendants will appear multiple times.
Context: inspect
Section: Classes and functions

Instruction: getclasstree(foo,unique=True)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Effect: Arrange the list of classes foo 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. Classes using multiple inheritance and their descendants will appear exactly one entry appears in the returned structure for each class in the given list.
Context: inspect
Section: Classes and functions

Instruction: get(full)argspec(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: Classes and functions
Returns: the names and default values of a the parameters of the function foo
Extra: deprecated without full
Typ: named tuple (Full)ArgSpec

Instruction: getargvalues(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: Classes and functions
Returns: information about arguments passed into a frame foo
Typ: A named tuple ArgInfo(args, varargs, keywords, locals)

Instruction: formatargspec(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: Classes and functions
Returns: Format a pretty argument spec from the values returned by getfullargspec().
Extra: Deprecated
Typ: String

Instruction: formatargvalues(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: Classes and functions
Returns: Format a pretty argument spec from the four values returned by getargvalues()
Typ: String

Instruction: getmro(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: Classes and functions
Returns: A tuple of class foo's base class, including foo, in method resolution order
Typ: String

Instruction: getcallargs(
  foo,bar_1,
  ...,
  bar_n
)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: Classes and functions
Returns: Bind the args and kwds bar_i's to the argument names of the function/method foo, as if it was called with them.
Typ: dict

Instruction: getclosurevars(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: Classes and functions
Returns: the mapping of external name references in a Python function/method foo to their current values
Typ: named tuple ClosureVars(nonlocals, globals, builtins, unbound)
Exception: TypeError is raised if foo is not a Python function or method.

Instruction: unwrap(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: Classes and functions
Returns: the object wrapped by the function foo. I.e. last object in chain of __wrapped__

Instruction: unwrap(
  foo,
  stop=bar
)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: Classes and functions
Returns: The first object in the chain of __wrapped__ satisfying stop, or the last element

Question: What is the danger with frame objects
Answer: It create a reference cycle, and thus lifespan of those object become much longer. Thus they must be destroyed explicitly when not used anymore
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Section: The interpreter stack
Context: inspect

Instruction: getframeinfo(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: The interpreter stack
Returns: information about a frame or traceback object foo

Instruction: getouterframes(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: The interpreter stack
Returns: a list of frame records for a frame and all outer frames. First entry represents foo, last entry represents the outermost call on foo's stack

Instruction: getinnerframes(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: The interpreter stack
Returns: a list of frame records for a traceback’s frame and all inner frames.

Instruction: currentframe()
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: The interpreter stack
Returns: the frame object for the caller’s stack frame. None if Python stack frame is not supported

Instruction: stack()
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: The interpreter stack
Returns: 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.

Instruction: trace()
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: The interpreter stack
Returns: 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.

Instruction: getattr_static(
  obj,
  attr,
  default=None
)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: The interpreter stack
Returns: Retrieve attributes without triggering dynamic lookup via the descriptor protocol, __getattr__(  ) or __getattribute__(  ).

Instruction: getgeneratorstate(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: Current State of Generators and Coroutines
Returns: Current state of generator-iterator foo

Instruction: getcoroutinestate(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: Current State of Generators and Coroutines
Returns: Current state of corountine object foo

Instruction: getgeneratorlocals(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: Current State of Generators and Coroutines
Returns: the mapping of live local variables in generator foo to their current values
Typ: Dic mapping variable names to values
Exception: TypeError is raised if foo is not a Python generator object.

Instruction: getcoroutinelocals(foo)
Page: https://docs.python.org/3/library/inspect.html#inspect.getmembers
Context: inspect
Section: Current State of Generators and Coroutines
Returns: This function is analogous to getgeneratorlocals(), but works for coroutine objects created by async def functions.