dir() List attribute names
Last updated: 2026-09-22
dir() List attribute names
dir() without args lists names in the current scope; with an object, returns its attribute and method names.
| Category | Built-in Functions |
|---|---|
| Kind | Built-in Function |
| Python Version | all |
📝 Syntax
dir([object])
⚙️ Parameters
| object Optional | Object whose names to list; omit to list the current scope.(Default:None) |
|---|
Returns:list. Names of attributes and methods (sorted by default).
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
['__doc__', '__loader__', '__name__', '__package__', '__spec__']
True
🏷️ Related Built-in Functions
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat is the difference between dir() with and without arguments?
AWithout arguments it lists the names defined in the current scope (variables, functions, classes, etc.); with an argument it lists all attribute and method names of that object.
QWhat type does dir() return?
AIt returns a list of strings sorted alphabetically; each element in sorted(dir(math)) is an attribute name.
QDoes dir(obj) list all attributes of obj?
AIt lists all findable attribute names on the object and its class inheritance chain (including private attributes with underscores), but dynamically generated attributes may be incomplete.
QWhat is the difference between dir() and vars()?
Adir() only returns a list of names (including inherited attribute names); vars(obj) returns the instance's own __dict__ dictionary (key-value pairs, not including inherited attributes).