dir(objeto)
The dir function returns the set of names associated with the object included as an argument. If no argument is included, returns the set of names in the local scope.
- object: object from which to obtain the associated names.
The result of the dir function is a list of text labels.
Let's import a library:
import time
If we now execute the dir function, we obtain the set of names associated with it:
dir(time)
['_STRUCT_TM_ITEMS',
'__Doc__',
'__loader__',
'__yam__',
'__package__',
'__spec__',
'altzone',
'asctime',
'ctime',
'daylight',
'get_clock_info',
'gmtime',
'localtime',
'mktime',
'monotonic',
'monotonic_ns',
'perf_counter',
'perf_counter_ns',
'process_time',
'process_time_ns',
'sleep',
'strftime',
'strptime',
'struct_time',
'thread_time',
'thread_time_ns',
'time',
'time_ns',
'timezone',
'tzname']
This list includes classes, functions, attributes, etc. contained in the library. If we use the print function, the result of the function is shown more compactly:
print(dir(time))
['_STRUCT_TM_ITEMS', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'altzone', 'asctime', 'ctime', 'daylight', 'get_clock_info', 'gmtime', ' localtime ',' mktime ',' monotonic ',' monotonic_ns', 'perf_counter', 'perf_counter_ns',' process_time ',' process_time_ns', 'sleep', 'strftime', 'strptime', 'struct_time', 'thread_time', 'thread_time_ns', 'time', 'time_ns', 'timezone', 'tzname']
If we pass to the function, for example, the name of a variable of a certain type, we obtain the associated names:
a = list([2, 4, 6])
print(dir(a))
['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
We see the methods associated with the lists that we usually use: count, index, insert, remove, etc.
Let's create a class to see the result of passing an object of the class to the dir function:
class Circle:
pi = 3.141592
def __init__(self, radio):
self.radio = radio
self.__color = "red"
def area(self):
return Circle.pi * (self.radio ** 2)
We have included a class attribute ("pi"), an attribute ("radius"), a private attribute ("__color") and a method ("area"). Now let's instantiate the class in the variable c:
c = Circle(3)
And let's execute the dir function passing the variable as an argument:
print(dir(c))
['_Circle__color', '__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__', 'area', 'pi', 'radio']
We see that the attribute "__color" appears with a new name ("_Circle__color") to avoid problems with the same attribute of the possible subclasses that may be created (process called name mangling), and the attributes "pi" and "radius" are shown, as well as the "area" method.