Introspection
Introspection
โน๏ธ
Checking id of object:
id()
Check that objects have same id:
is
Checking the type of object/variable:
type()
- returns a type of objectisinstance(x, typeA)
- returns True/False depends if objectx
of typetypeA
dir()
- return a list of valid attributes for argument (or list of names in current local scope if no argument)sys.getsizeof()
- get the size (in bytes) of the memory allocated byt the objectmodule
inspect
- low-level API for contents of a class, source code of a method, argument list for a function, detailed tracebackmodule
dis
- decompiling Python byte-code showing code execution trace3rd-party module
rich
has handlyinspect
method for inspecting any kind of Python object
Example of introspection of int object:
๐ช Code:
a = 42
print(dir(a))
๐ Output:
['__abs__', '__add__', '__and__', '__bool__', '__ceil__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floor__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__index__', '__init__', '__init_subclass__', '__int__', '__invert__', '__le__', '__lshift__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__or__', '__pos__', '__pow__', '__radd__', '__rand__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rlshift__', '__rmod__', '__rmul__', '__ror__', '__round__', '__rpow__', '__rrshift__', '__rshift__', '__rsub__', '__rtruediv__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', '__xor__', 'as_integer_ratio', 'bit_count', 'bit_length', 'conjugate', 'denominator', 'from_bytes', 'imag', 'numerator', 'real', 'to_bytes']
We see a lot of methods available in object which gives us a hint what is the kind of object it is and what we can do with it.
Introspection of an instance of some class:
๐ช Code:
class A(object): # Creating simple class
attr1 = 5 # with one attribute: "attr1"
some_obj = A()
print(some_obj.attr1) # Checking the value of custom attribute
print(dir(some_obj)) # This will show all inherited methods and attribute we created: "attr1"
๐ Output:
5
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'attr1']
In this case we can see many inherited methods (from parent class called "object") and also attributes and methods defined by us (in this example it is just one attribute "attr1")
Getting the size of an object
sys.getsizeof()
- get the size (in bytes) of the memory allocated byt the object.
๐ช Code:
import sys
sys.getsizeof(100500)
๐ Output:
28
## Introspection with `rich`
๐ฅ
There is a nice library rich
used for displaying various content to terminal. It is can be used as additional inspection tool in Python (or ipython
/Jupyter
also):
from rich import inspect
i = 100500
inspect(i)
โญโโโโโโ <class 'int'> โโโโโโโโฎ
โ int([x]) -> integer โ
โ int(x, base=10) -> integer โ
โ โ
โ โญโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ
โ โ 100500 โ โ
โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ
โ โ
โ denominator = 1 โ
โ imag = 0 โ
โ numerator = 100500 โ
โ real = 100500 โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
๐ฅ
And with methods overview:
inspect(i, methods=True)
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ <class 'int'> โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ int([x]) -> integer โ
โ int(x, base=10) -> integer โ
โ โ
โ โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ
โ โ 100500 โ โ
โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ
โ โ
โ denominator = 1 โ
โ imag = 0 โ
โ numerator = 100500 โ
โ real = 100500 โ
โ as_integer_ratio = def as_integer_ratio(): Return integer ratio. โ
โ bit_count = def bit_count(): Number of ones in the binary representation of the absolute value of self. โ
โ bit_length = def bit_length(): Number of bits necessary to represent self in binary. โ
โ conjugate = def conjugate(...) Returns self, the complex conjugate of any int. โ
โ from_bytes = def from_bytes(bytes, byteorder, *, signed=False): Return the integer represented by the โ
โ given array of bytes. โ
โ to_bytes = def to_bytes(length, byteorder, *, signed=False): Return an array of bytes representing an โ
โ integer. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
๐ฅ
An example of list
inspection:
inspect([1, 2, 3], methods=True)
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ <class 'list'> โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ Built-in mutable sequence. โ
โ โ
โ โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ
โ โ [1, 2, 3] โ โ
โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ
โ โ
โ append = def append(object, /): Append object to the end of the list. โ
โ clear = def clear(): Remove all items from list. โ
โ copy = def copy(): Return a shallow copy of the list. โ
โ count = def count(value, /): Return number of occurrences of value. โ
โ extend = def extend(iterable, /): Extend list by appending elements from the iterable. โ
โ index = def index(value, start=0, stop=9223372036854775807, /): Return first index of value. โ
โ insert = def insert(index, object, /): Insert object before index. โ
โ pop = def pop(index=-1, /): Remove and return item at index (default last). โ
โ remove = def remove(value, /): Remove first occurrence of value. โ
โ reverse = def reverse(): Reverse *IN PLACE*. โ
โ sort = def sort(*, key=None, reverse=False): Sort the list in ascending order and return None. โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
Basic types of objects in Python
Mutable
Object can be changed after creation
list, dict, set, bytearray
Immutable
Object can not be changed after creation
int, float, complex, str, tuple, frozenset
Sequence (collection, iterable)
Object can holds other object in itself (has magic methods like __getitem__()
)
list, tuple, set, str, frozenset, dict
Ordered
Members are ordered
list
, str
, tuple
, dict
Unordered
Members are unordered
set
, frozenset
Additional categories:
Hashable
Object that can be a key to dictionary (has __hash__()
- all immutable and instances of custom classes)
tuple, int, float, str, frozenset, object
Iterable
Object capable of returning it's member one at a time (has __iter__()
or __getitem__()
)
str, list, tuple, set, frozenset, dict
Callable
Object that can behave as function (has __call__()
method defined)
class
, function
, method
Last updated
Was this helpful?