xoutil.inspect – Inspect live objects

Extensions to Python’s inspect module.

You may use it as drop-in replacement of inspect. Although we don’t document all items here. Refer to inspect's documentation.

class xoutil.inspect.FullArgSpec(args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations)
annotations

Alias for field number 6

args

Alias for field number 0

defaults

Alias for field number 3

kwonlyargs

Alias for field number 4

kwonlydefaults

Alias for field number 5

varargs

Alias for field number 1

varkw

Alias for field number 2

xoutil.inspect.get_attr_value(obj, name, *default)[source]

Get a named attribute from an object in a safe way.

Similar to getattr but without triggering dynamic look-up via the descriptor protocol, __getattr__ or __getattribute__ by using getattr_static().

xoutil.inspect.getattr_static(obj, attr, default=<object object>)[source]

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 descriptor objects instead of instance members in some cases. See the documentation for details.

xoutil.inspect.type_name(obj, affirm=False)[source]

Return the internal name for a type or a callable.

This function is safe. If :param obj: is not an instance of a proper type then returns the following depending on :param affirm:

  • If False returns None.
  • If True convert a single object to its type before returns the name, but if is a tuple, list or set; returns a string with a representation of contained types.

Examples:

>>> type_name(int)
'int'

>>> type_name(0) is None
True

>>> type_name(0, affirm=True)
'int'

>>> type_name((0, 1.1)) is None
True

>>> type_name((0, 1.1), affirm=True)
'(int, float)'