Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's is meant by the dir() built-in function returns "(some of) the attributes of the given object"?

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.
like image 599
Shuzheng Avatar asked Nov 17 '20 08:11

Shuzheng


People also ask

What is the dir () meant to do in Python?

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.

Which built in function will return all the methods or attribute of an object?

You can use the built in dir() function to get a list of all the attributes a module has.

What is the purpose of the dir function and the help function?

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.

Which function returns the list of attributes and methods?

li is a list, so dir(li) returns a list of all the methods of a list.

What is the return type of dir () function?

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.

What is the use of Dir in Python?

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.

What is the use of Dirdir?

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.

What is the use of the return function?

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.


1 Answers

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 dirdir 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)

like image 99
MisterMiyagi Avatar answered Oct 18 '22 15:10

MisterMiyagi