Python __dir__()
__dir__()
is a built-in function in Python that returns a list of names in the current local scope or an object's attributes, depending on the context in which it is called.
When __dir__()
is called without any arguments, it returns a list of names of the attributes and methods in the current local scope. For example:
print(__dir__())
This will print a list of names in the current local scope, including built-in names, global names, and names defined in the current module.
When __dir__()
is called on an object, it returns a list of valid attributes and methods that can be accessed on that object. For example:
my_list = [1, 2, 3]
print(my_list.__dir__())
This will print a list of attributes and methods that can be used on the my_list
object, such as append()
, count()
, and sort()
.
Note that __dir__()
is typically called implicitly when using the dir()
function. For example:
print(dir(my_list))
This will have the same effect as calling my_list.__dir__()
.