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.

CategoryBuilt-in Functions
KindBuilt-in Function
Python Versionall

📝 Syntax

dir([object])

⚙️ Parameters

object OptionalObject 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
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).
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏