English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
dir() method attempts to return a list of valid attribute lists of the object.
The syntax of dir() is:
dir([object])
dir() can contain at most one object.
Object(Optional)-dir() attempts to return all attributes of this object.
dir() attempts to return a list of valid attributes of the object.
If the object has a __dir__() method, that method is called, and it must return a list of attributes.
If the object does not have a __dir__() method, this method attempts to find information from the __dict__ attribute (if defined) and the type object. In this case, the list returned from dir() may be incomplete.
If an object is not passed to the dir() method, it will return a list of names in the current local scope.
number = [1, 2, 3] print(dir(number)) print('\nReturn Value from empty dir()') print(dir())
When running the program, the output is:
__add__, __class__, __contains__, __delattr__, __delitem__, __dir__, __doc__, __eq__, __format__, __ge__, __getattribute__, __getitem__, __gt__, __hash__, __iadd__, __imul__, __init__, __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 Return Value from Empty dir() __builtins__, number
class Person: def __dir__(self): return ['age', 'name', 'salary'] teacher = Person() print(dir(teacher))
When running the program, the output is:
['age', 'name', 'salary']