doc_content
stringlengths
1
386k
doc_id
stringlengths
5
188
classmethod factory(loader) A static method which returns a callable that creates a lazy loader. This is meant to be used in situations where the loader is passed by class instead of by instance. suffixes = importlib.machinery.SOURCE_SUFFIXES loader = importlib.machinery.SourceFileLoader lazy_loader = importlib.util....
python.library.importlib#importlib.util.LazyLoader.factory
importlib.util.MAGIC_NUMBER The bytes which represent the bytecode version number. If you need help with loading/writing bytecode then consider importlib.abc.SourceLoader. New in version 3.4.
python.library.importlib#importlib.util.MAGIC_NUMBER
@importlib.util.module_for_loader A decorator for importlib.abc.Loader.load_module() to handle selecting the proper module object to load with. The decorated method is expected to have a call signature taking two positional arguments (e.g. load_module(self, module)) for which the second argument will be the module ob...
python.library.importlib#importlib.util.module_for_loader
importlib.util.module_from_spec(spec) Create a new module based on spec and spec.loader.create_module. If spec.loader.create_module does not return None, then any pre-existing attributes will not be reset. Also, no AttributeError will be raised if triggered while accessing spec or setting an attribute on the module. ...
python.library.importlib#importlib.util.module_from_spec
importlib.util.resolve_name(name, package) Resolve a relative module name to an absolute one. If name has no leading dots, then name is simply returned. This allows for usage such as importlib.util.resolve_name('sys', __spec__.parent) without doing a check to see if the package argument is needed. ImportError is rais...
python.library.importlib#importlib.util.resolve_name
@importlib.util.set_loader A decorator for importlib.abc.Loader.load_module() to set the __loader__ attribute on the returned module. If the attribute is already set the decorator does nothing. It is assumed that the first positional argument to the wrapped method (i.e. self) is what __loader__ should be set to. Cha...
python.library.importlib#importlib.util.set_loader
@importlib.util.set_package A decorator for importlib.abc.Loader.load_module() to set the __package__ attribute on the returned module. If __package__ is set and has a value other than None it will not be changed. Deprecated since version 3.4: The import machinery takes care of this automatically.
python.library.importlib#importlib.util.set_package
importlib.util.source_from_cache(path) Given the path to a PEP 3147 file name, return the associated source code file path. For example, if path is /foo/bar/__pycache__/baz.cpython-32.pyc the returned path would be /foo/bar/baz.py. path need not exist, however if it does not conform to PEP 3147 or PEP 488 format, a V...
python.library.importlib#importlib.util.source_from_cache
importlib.util.source_hash(source_bytes) Return the hash of source_bytes as bytes. A hash-based .pyc file embeds the source_hash() of the corresponding source file’s contents in its header. New in version 3.7.
python.library.importlib#importlib.util.source_hash
importlib.util.spec_from_file_location(name, location, *, loader=None, submodule_search_locations=None) A factory function for creating a ModuleSpec instance based on the path to a file. Missing information will be filled in on the spec by making use of loader APIs and by the implication that the module will be file-...
python.library.importlib#importlib.util.spec_from_file_location
importlib.util.spec_from_loader(name, loader, *, origin=None, is_package=None) A factory function for creating a ModuleSpec instance based on a loader. The parameters have the same meaning as they do for ModuleSpec. The function uses available loader APIs, such as InspectLoader.is_package(), to fill in any missing in...
python.library.importlib#importlib.util.spec_from_loader
importlib.__import__(name, globals=None, locals=None, fromlist=(), level=0) An implementation of the built-in __import__() function. Note Programmatic importing of modules should use import_module() instead of this function.
python.library.importlib#importlib.__import__
exception ImportWarning Base class for warnings about probable mistakes in module imports. Ignored by the default warning filters. Enabling the Python Development Mode shows this warning.
python.library.exceptions#ImportWarning
exception IndentationError Base class for syntax errors related to incorrect indentation. This is a subclass of SyntaxError.
python.library.exceptions#IndentationError
exception IndexError Raised when a sequence subscript is out of range. (Slice indices are silently truncated to fall in the allowed range; if an index is not an integer, TypeError is raised.)
python.library.exceptions#IndexError
input([prompt]) If the prompt argument is present, it is written to standard output without a trailing newline. The function then reads a line from input, converts it to a string (stripping a trailing newline), and returns that. When EOF is read, EOFError is raised. Example: >>> s = input('--> ') --> Monty Python's...
python.library.functions#input
inspect — Inspect live objects Source code: 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...
python.library.inspect
class inspect.BoundArguments Result of a Signature.bind() or Signature.bind_partial() call. Holds the mapping of arguments to the function’s parameters. arguments A mutable mapping of parameters’ names to arguments’ values. Contains only explicitly bound arguments. Changes in arguments will reflect in args and kw...
python.library.inspect#inspect.BoundArguments
apply_defaults() Set default values for missing arguments. For variable-positional arguments (*args) the default is an empty tuple. For variable-keyword arguments (**kwargs) the default is an empty dict. >>> def foo(a, b='ham', *args): pass >>> ba = inspect.signature(foo).bind('spam') >>> ba.apply_defaults() >>> ba.a...
python.library.inspect#inspect.BoundArguments.apply_defaults
args A tuple of positional arguments values. Dynamically computed from the arguments attribute.
python.library.inspect#inspect.BoundArguments.args
arguments A mutable mapping of parameters’ names to arguments’ values. Contains only explicitly bound arguments. Changes in arguments will reflect in args and kwargs. Should be used in conjunction with Signature.parameters for any argument processing purposes. Note Arguments for which Signature.bind() or Signature.b...
python.library.inspect#inspect.BoundArguments.arguments
kwargs A dict of keyword arguments values. Dynamically computed from the arguments attribute.
python.library.inspect#inspect.BoundArguments.kwargs
signature A reference to the parent Signature object.
python.library.inspect#inspect.BoundArguments.signature
inspect.cleandoc(doc) Clean up indentation from docstrings that are indented to line up with blocks of code. 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 remove...
python.library.inspect#inspect.cleandoc
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. New in version 3.6.
python.library.inspect#inspect.CO_ASYNC_GENERATOR
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. New in version 3.5.
python.library.inspect#inspect.CO_COROUTINE
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.
python.library.inspect#inspect.CO_GENERATOR
inspect.CO_ITERABLE_COROUTINE The flag is used to transform generators into generator-based coroutines. Generator objects with this flag can be used in await expression, and can yield from coroutine objects. See PEP 492 for more details. New in version 3.5.
python.library.inspect#inspect.CO_ITERABLE_COROUTINE
inspect.CO_NESTED The flag is set when the code object is a nested function.
python.library.inspect#inspect.CO_NESTED
inspect.CO_NEWLOCALS If set, a new dict will be created for the frame’s f_locals when the code object is executed.
python.library.inspect#inspect.CO_NEWLOCALS
inspect.CO_NOFREE The flag is set if there are no free or cell variables.
python.library.inspect#inspect.CO_NOFREE
inspect.CO_OPTIMIZED The code object is optimized, using fast locals.
python.library.inspect#inspect.CO_OPTIMIZED
inspect.CO_VARARGS The code object has a variable positional parameter (*args-like).
python.library.inspect#inspect.CO_VARARGS
inspect.CO_VARKEYWORDS The code object has a variable keyword parameter (**kwargs-like).
python.library.inspect#inspect.CO_VARKEYWORDS
inspect.currentframe() Return the frame object for the caller’s stack frame. CPython implementation detail: 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 ...
python.library.inspect#inspect.currentframe
inspect.formatargspec(args[, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations[, formatarg, formatvarargs, formatvarkw, formatvalue, formatreturns, formatannotations]]) Format a pretty argument spec from the values returned by getfullargspec(). The first seven arguments are (args, varargs, varkw, defa...
python.library.inspect#inspect.formatargspec
inspect.formatargvalues(args[, varargs, varkw, locals, formatarg, formatvarargs, formatvarkw, formatvalue]) Format a pretty argument spec from the four values returned by getargvalues(). The format* arguments are the corresponding optional formatting functions that are called to turn names and values into strings. N...
python.library.inspect#inspect.formatargvalues
inspect.getargspec(func) Get the names and default values of a Python function’s parameters. A named tuple ArgSpec(args, varargs, keywords, defaults) is returned. args is a list of the parameter names. varargs and keywords are the names of the * and ** parameters or None. defaults is a tuple of default argument value...
python.library.inspect#inspect.getargspec
inspect.getargvalues(frame) Get information about arguments passed into a particular frame. A named tuple ArgInfo(args, varargs, keywords, locals) is returned. args is a list of the argument names. varargs and keywords are the names of the * and ** arguments or None. locals is the locals dictionary of the given frame...
python.library.inspect#inspect.getargvalues
inspect.getattr_static(obj, attr, default=None) 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 th...
python.library.inspect#inspect.getattr_static
inspect.getcallargs(func, /, *args, **kwds) Bind the args and kwds to the argument names of the Python function or method func, as if it was called with them. For bound methods, bind also the first argument (typically named self) to the associated instance. A dict is returned, mapping the argument names (including th...
python.library.inspect#inspect.getcallargs
inspect.getclasstree(classes, unique=False) 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 unique a...
python.library.inspect#inspect.getclasstree
inspect.getclosurevars(func) Get the mapping of external name references in a Python function or method func to their current values. A named tuple ClosureVars(nonlocals, globals, builtins, unbound) is returned. nonlocals maps referenced names to lexical closure variables, globals to the function’s module globals and...
python.library.inspect#inspect.getclosurevars
inspect.getcomments(object) 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 None. This could happen if the object h...
python.library.inspect#inspect.getcomments
inspect.getcoroutinelocals(coroutine) This function is analogous to getgeneratorlocals(), but works for coroutine objects created by async def functions. New in version 3.5.
python.library.inspect#inspect.getcoroutinelocals
inspect.getcoroutinestate(coroutine) Get current state of a coroutine object. The function is intended to be used with coroutine objects created by async def functions, but will accept any coroutine-like object that has cr_running and cr_frame attributes. Possible states are: CORO_CREATED: Waiting to start executi...
python.library.inspect#inspect.getcoroutinestate
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. Changed in version 3.5: Documenta...
python.library.inspect#inspect.getdoc
inspect.getfile(object) Return the name of the (text or binary) file in which an object was defined. This will fail with a TypeError if the object is a built-in module, class, or function.
python.library.inspect#inspect.getfile
inspect.getframeinfo(frame, context=1) Get information about a frame or traceback object. A named tuple Traceback(filename, lineno, function, code_context, index) is returned.
python.library.inspect#inspect.getframeinfo
inspect.getfullargspec(func) Get the names and default values of a Python function’s parameters. A named tuple is returned: FullArgSpec(args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations) args is a list of the positional parameter names. varargs is the name of the * parameter or None if arbitrary...
python.library.inspect#inspect.getfullargspec
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 wit...
python.library.inspect#inspect.getgeneratorlocals
inspect.getgeneratorstate(generator) Get current state of a generator-iterator. Possible states are: GEN_CREATED: Waiting to start execution. GEN_RUNNING: Currently being executed by the interpreter. GEN_SUSPENDED: Currently suspended at a yield expression. GEN_CLOSED: Execution has completed. New in version 3...
python.library.inspect#inspect.getgeneratorstate
inspect.getinnerframes(traceback, context=1) Get a list of frame records for a traceback’s frame and all inner frames. These frames represent calls made as a consequence of frame. The first entry in the list represents traceback; the last entry represents where the exception was raised. Changed in version 3.5: A lis...
python.library.inspect#inspect.getinnerframes
inspect.getmembers(object[, predicate]) Return all the members of an object in a list of (name, value) pairs sorted by name. If the optional predicate argument—which will be called with the value object of each member—is supplied, only members for which the predicate returns a true value are included. Note getmember...
python.library.inspect#inspect.getmembers
inspect.getmodule(object) Try to guess which module an object was defined in.
python.library.inspect#inspect.getmodule
inspect.getmodulename(path) Return the name of the module named by the file path, without including the names of enclosing packages. The file extension is checked against all of the entries in importlib.machinery.all_suffixes(). If it matches, the final path component is returned with the extension removed. Otherwise...
python.library.inspect#inspect.getmodulename
inspect.getmro(cls) 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.
python.library.inspect#inspect.getmro
inspect.getouterframes(frame, context=1) Get a list of frame records for a frame and all outer frames. These frames represent the calls that lead to the creation of frame. The first entry in the returned list represents frame; the last entry represents the outermost call on frame’s stack. Changed in version 3.5: A l...
python.library.inspect#inspect.getouterframes
inspect.getsource(object) 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 OSError is raised if the source code cannot be retrieved. Changed in version 3.3: OSError is raised i...
python.library.inspect#inspect.getsource
inspect.getsourcefile(object) Return the name of the Python source file in which an object was defined. This will fail with a TypeError if the object is a built-in module, class, or function.
python.library.inspect#inspect.getsourcefile
inspect.getsourcelines(object) 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 origin...
python.library.inspect#inspect.getsourcelines
inspect.isabstract(object) Return True if the object is an abstract base class.
python.library.inspect#inspect.isabstract
inspect.isasyncgen(object) Return True if the object is an asynchronous generator iterator created by an asynchronous generator function. New in version 3.6.
python.library.inspect#inspect.isasyncgen
inspect.isasyncgenfunction(object) Return True if the object is an asynchronous generator function, for example: >>> async def agen(): ... yield 1 ... >>> inspect.isasyncgenfunction(agen) True New in version 3.6. Changed in version 3.8: Functions wrapped in functools.partial() now return True if the wrapped f...
python.library.inspect#inspect.isasyncgenfunction
inspect.isawaitable(object) Return True if the object can be used in await expression. Can also be used to distinguish generator-based coroutines from regular generators: def gen(): yield @types.coroutine def gen_coro(): yield assert not isawaitable(gen()) assert isawaitable(gen_coro()) New in version 3.5.
python.library.inspect#inspect.isawaitable
inspect.isbuiltin(object) Return True if the object is a built-in function or a bound built-in method.
python.library.inspect#inspect.isbuiltin
inspect.isclass(object) Return True if the object is a class, whether built-in or created in Python code.
python.library.inspect#inspect.isclass
inspect.iscode(object) Return True if the object is a code.
python.library.inspect#inspect.iscode
inspect.iscoroutine(object) Return True if the object is a coroutine created by an async def function. New in version 3.5.
python.library.inspect#inspect.iscoroutine
inspect.iscoroutinefunction(object) Return True if the object is a coroutine function (a function defined with an async def syntax). New in version 3.5. Changed in version 3.8: Functions wrapped in functools.partial() now return True if the wrapped function is a coroutine function.
python.library.inspect#inspect.iscoroutinefunction
inspect.isdatadescriptor(object) Return True if the object is a data descriptor. Data descriptors have a __set__ or a __delete__ 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 acros...
python.library.inspect#inspect.isdatadescriptor
inspect.isframe(object) Return True if the object is a frame.
python.library.inspect#inspect.isframe
inspect.isfunction(object) Return True if the object is a Python function, which includes functions created by a lambda expression.
python.library.inspect#inspect.isfunction
inspect.isgenerator(object) Return True if the object is a generator.
python.library.inspect#inspect.isgenerator
inspect.isgeneratorfunction(object) Return True if the object is a Python generator function. Changed in version 3.8: Functions wrapped in functools.partial() now return True if the wrapped function is a Python generator function.
python.library.inspect#inspect.isgeneratorfunction
inspect.isgetsetdescriptor(object) Return True if the object is a getset descriptor. CPython implementation detail: getsets are attributes defined in extension modules via PyGetSetDef structures. For Python implementations without such types, this method will always return False.
python.library.inspect#inspect.isgetsetdescriptor
inspect.ismemberdescriptor(object) Return True if the object is a member descriptor. CPython implementation detail: Member descriptors are attributes defined in extension modules via PyMemberDef structures. For Python implementations without such types, this method will always return False.
python.library.inspect#inspect.ismemberdescriptor
inspect.ismethod(object) Return True if the object is a bound method written in Python.
python.library.inspect#inspect.ismethod
inspect.ismethoddescriptor(object) Return True if the object is a method descriptor, but not if ismethod(), isclass(), isfunction() or isbuiltin() are true. This, for example, is true of int.__add__. An object passing this test has a __get__() method but not a __set__() method, but beyond that the set of attributes v...
python.library.inspect#inspect.ismethoddescriptor
inspect.ismodule(object) Return True if the object is a module.
python.library.inspect#inspect.ismodule
inspect.isroutine(object) Return True if the object is a user-defined or built-in function or method.
python.library.inspect#inspect.isroutine
inspect.istraceback(object) Return True if the object is a traceback.
python.library.inspect#inspect.istraceback
class inspect.Parameter(name, kind, *, default=Parameter.empty, annotation=Parameter.empty) Parameter objects are immutable. Instead of modifying a Parameter object, you can use Parameter.replace() to create a modified copy. Changed in version 3.5: Parameter objects are picklable and hashable. empty A special c...
python.library.inspect#inspect.Parameter
annotation The annotation for the parameter. If the parameter has no annotation, this attribute is set to Parameter.empty.
python.library.inspect#inspect.Parameter.annotation
default The default value for the parameter. If the parameter has no default value, this attribute is set to Parameter.empty.
python.library.inspect#inspect.Parameter.default
empty A special class-level marker to specify absence of default values and annotations.
python.library.inspect#inspect.Parameter.empty
kind Describes how argument values are bound to the parameter. Possible values (accessible via Parameter, like Parameter.KEYWORD_ONLY): Name Meaning POSITIONAL_ONLY Value must be supplied as a positional argument. Positional only parameters are those which appear before a / entry (if present) in a Python functi...
python.library.inspect#inspect.Parameter.kind
kind.description Describes a enum value of Parameter.kind. New in version 3.8. Example: print all descriptions of arguments: >>> 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 keywo...
python.library.inspect#inspect.Parameter.kind.description
name The name of the parameter as a string. The name must be a valid Python identifier. CPython implementation detail: CPython generates implicit parameter names of the form .0 on the code objects used to implement comprehensions and generator expressions. Changed in version 3.6: These parameter names are exposed b...
python.library.inspect#inspect.Parameter.name
replace(*[, name][, kind][, default][, annotation]) Create a new Parameter instance based on the instance replaced was invoked on. To override a Parameter attribute, pass the corresponding argument. To remove a default value or/and an annotation from a Parameter, pass Parameter.empty. >>> from inspect import Paramete...
python.library.inspect#inspect.Parameter.replace
class inspect.Signature(parameters=None, *, return_annotation=Signature.empty) A Signature object represents the call signature of a function and its return annotation. For each parameter accepted by the function it stores a Parameter object in its parameters collection. The optional parameters argument is a sequence...
python.library.inspect#inspect.Signature
inspect.signature(callable, *, follow_wrapped=True) Return a Signature object for the given callable: >>> 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'].anno...
python.library.inspect#inspect.signature
bind(*args, **kwargs) Create a mapping from positional and keyword arguments to parameters. Returns BoundArguments if *args and **kwargs match the signature, or raises a TypeError.
python.library.inspect#inspect.Signature.bind
bind_partial(*args, **kwargs) Works the same way as Signature.bind(), 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.
python.library.inspect#inspect.Signature.bind_partial
empty A special class-level marker to specify absence of a return annotation.
python.library.inspect#inspect.Signature.empty
classmethod from_callable(obj, *, follow_wrapped=True) Return a Signature (or its subclass) object for a given callable obj. Pass follow_wrapped=False to get a signature of obj without unwrapping its __wrapped__ chain. This method simplifies subclassing of Signature: class MySignature(Signature): pass sig = MySig...
python.library.inspect#inspect.Signature.from_callable
parameters An ordered mapping of parameters’ names to the corresponding Parameter objects. Parameters appear in strict definition order, including keyword-only parameters. Changed in version 3.7: Python only explicitly guaranteed that it preserved the declaration order of keyword-only parameters as of version 3.7, a...
python.library.inspect#inspect.Signature.parameters
replace(*[, parameters][, return_annotation]) Create a new Signature instance based on the instance replace was invoked on. 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 i...
python.library.inspect#inspect.Signature.replace
return_annotation The “return” annotation for the callable. If the callable has no “return” annotation, this attribute is set to Signature.empty.
python.library.inspect#inspect.Signature.return_annotation
inspect.stack(context=1) 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. Changed in version 3.5: A list of named tuples FrameInfo(frame, filename, lineno, function, code_context, index) is re...
python.library.inspect#inspect.stack