You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中__dir__方法的具体实现及查询方式

Awesome question! You're right that the _dir_object function just delegates to the object's __dir__ method—so let's dig into how to find the actual implementation of __dir__ for different types of objects in CPython.

How to Find __dir__ Implementations in CPython

The implementation of __dir__ varies depending on what kind of object you're dealing with. Here's how to track it down:

1. Built-in Types (like list, dict, str)

For core Python types, their __dir__ methods are implemented in C. Follow these steps:

  • Navigate to the Objects/ directory in the CPython repo—each built-in type has its own dedicated file (e.g., listobject.c for lists, dictobject.c for dicts).
  • Locate the PyTypeObject struct for the target type (e.g., PyList_Type for lists). Look for the tp_dir field—this points directly to the C function that implements __dir__ for that type.
  • Search for the function name linked to tp_dir to view the full implementation code.

For example:

  • For list, PyList_Type.tp_dir points to list_dir. This function collects standard list methods, plus any extra attributes stored in the list instance's __dict__ (common if you've subclassed list and added custom attributes).
  • If a type doesn't define a custom tp_dir, it falls back to the generic PyObject_Dir function (in object.c), which handles gathering attributes from the object's class hierarchy and its instance __dict__.

2. Custom Python Classes

  • If you've explicitly defined a __dir__ method in your Python class, the implementation is right there in your code—no digging needed!
  • If you haven't overridden __dir__, your class inherits it from its parent class. For most classes, this eventually traces back to the base object class's __dir__, implemented in the object_dir function in object.c. This function:
    • Collects attributes from the object's instance __dict__
    • Recursively gathers attributes from the class and all its base classes
    • Deduplicates and sorts the combined list before returning it

3. Extension Types (C-written third-party libraries)

For types from C-based third-party extensions, you'll need to inspect the library's source code. Find the PyTypeObject struct for the target type, check its tp_dir field, then look up the corresponding function implementation in the library's codebase.

Quick Deep Dive: object.__dir__ Logic

Since most default __dir__ implementations rely on object_dir, here's a simplified breakdown of its core logic:

The object_dir function first fetches the object's class, then walks up the inheritance chain to collect all class-level attributes. It then adds any keys from the object's __dict__ (if the object supports instance dictionaries). Finally, it sorts and deduplicates the combined list to produce the final set of names you see when calling dir().

内容的提问来源于stack exchange,提问作者laike9m

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:35:57