Skip to content

Information about Objects

These functions are designed to inspect and understand objects at runtime. They are especially useful during code development, debugging, and documentation. They allow you to know the type of an object, its location in memory, or access its built-in help. Using them facilitates safer and more maintainable development, especially in dynamic environments.

FunctionDescription
type()Returns the type of the specified object.
id()Returns the address or unique identifier of the object in memory.
help()Shows the built-in documentation (docstring) of functions, objects, modules, etc.
dir()Lists the attributes and methods available for an object.

Below I will explain in more detail how these functions work:

Typical use: To verify if a variable is of the expected type before performing operations with it.

type.py
x = 42
print(type(x))

Output:

type.py
<class 'int'>

Typical use: To check if two variables point to the same object in memory.

id.py
a = 10
b = a
print(id(a))
print(id(b)) # Same ID as a, since both point to the same object

Output:

id.py
140710837832368 (example value)

This will show all the available methods for string (str) objects, such as .upper(), .lower(), .split(), etc.

dir.py
print(dir(str))

Output:

dir.py
['__add__', '__class__', '__contains__', '__delattr__', '__dir__',
'__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__',
'__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__',
'__ne__', '__new__', '__reduce__', '__repr__', '__rmod__', '__rmul__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__',
'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith',
'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isascii',
'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric',
'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower',
'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition',
'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase',
'title', 'translate', 'upper', 'zfill']

Typical use: During the exploration or learning of a function or method. Very useful in interactive environments such as the terminal or Jupyter Notebook.

help.py
help(str.upper)

Output:

help.py
Help on method_descriptor:
upper(self, /)
Return a copy of the string converted to uppercase.