I wonder whether the documentation is wrong for the dir()
built-in function. In particular, what object attributes may not be part of the list returned by dir()
?
For both class objects and other objects, the documentation says that the list contains "its attributes", which means a full set (and not "some of") of attributes?
Python v. 3.9 @ macOS 10.15.7
Help on built-in function dir in module builtins:
dir(...)
dir([object]) -> list of strings
If called without an argument, return the names in the current scope.
Else, return an alphabetized list of names comprising (some of) the attributes
of the given object, and of attributes reachable from it.
If the object supplies a method named __dir__, it will be used; otherwise
the default dir() logic is used and returns:
for a module object: the module's attributes.
for a class object: its attributes, and recursively the attributes
of its bases.
for any other object: its attributes, its class's attributes, and
recursively the attributes of its class's base classes.
The dir() function returns all properties and methods of the specified object, without the values. This function will return all the properties and methods, even built-in properties which are the default for all object.
You can use the built in dir() function to get a list of all the attributes a module has.
Help() and dir(), are the two functions that are reachable from the python interpreter. Both functions are utilized for observing the combine dump of build-in-function. These created functions in python are truly helpful for the efficient observation of the built-in system.
li is a list, so dir(li) returns a list of all the methods of a list.
The function returns an object of type list. In this example, we will pass an object to the dir () function, and observe the list returned by it. In this example, we will not pass any object to the dir () function, and observe the list returned by it. Of course, we have two variables defined in the local scope.
dir () function in Python. dir () is a powerful inbuilt function in Python3, which returns list of the attributes and methods of any object (say functions , modules, strings, lists, dictionaries etc.) dir () tries to return a valid list of attributes of the object it is called upon.
dir() is a powerful inbuilt function in Python3, which returns list of the attributes and methods of any object (say functions , modules, strings, lists, dictionaries etc.) Syntax : Parameters : Returns : dir() tries to return a valid list of attributes of the object it is called upon.
This function will return all the properties and methods, even built-in properties which are default for all object. W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
In Python, an attribute is not necessarily a thing on an object, or a field on the class. For example, __getattr__
can define arbitrary attributes on-demand. As such, there is no general, robust way to discover an attribute without actually accessing it.
If the object does not provide
__dir__()
, the function tries its best to gather information from the object’s__dict__
attribute, if defined, and from its type object. The resulting list is not necessarily complete, and may be inaccurate when the object has a custom__getattr__()
.
There is no fixed set of attributes which are not covered by dir
– dir
uses a heuristic and the __dir__
hook, so any attribute may-or-may-not be discovered. In general, for a well-behaved object one can expect public attributes to be visible to dir
. Private and especially special attributes are candidates to be excluded from dir
.
>>> '__dict__' in dir(object)
False
>>> hasattr(object, '__dict__')
True
Note: Because
dir()
is supplied primarily as a convenience for use at an interactive prompt, it tries to supply an interesting set of names more than it tries to supply a rigorously or consistently defined set of names, and its detailed behavior may change across releases. For example, metaclass attributes are not in the result list when the argument is a class.
(All quotes from Documentation » The Python Standard Library » Built-in Functions: dir)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With