python : object.__dict__ 未显示所有属性

python : object.__dict__ not showing all attributes

我正在尝试打印一个对象的所有属性。目的主要是为了调试。

我正在使用以下代码:

import json
print(json.dumps(object.__dict__), indent=4, sort_keys=True, default=str))

打印对象的属性。

但我正在使用

检查对象的所有属性列表
dir(object)

我发现 __dict__ 根本没有显示 dir() 列出的某些属性。

我想过使用 dir(object) 但它只是一个没有属性的属性列表。

所以我认为:

`__dict__` has `incomplete` properties but with attirbutes

`dir()` has complete list of properties but no attibutes

那么获取对象属性的完整字典的最佳方法是什么,这样我就可以使用 json.dumps

进行漂亮的打印和查看

示例

我在 django 项目中工作。这与数据库连接有关

所以这个例子是一个变量名db_con,它属于以下对象

<django.db.backends.postgresql.base.DatabaseWrapper object at 0x7f29dee47a58>

__dict__ 方式

import json
print(json.dumps(db_con.__dict__), indent=4, sort_keys=True, default=str))

输出为

{
    "_thread_ident": ATTRIBUTE_VALUE
    "_thread_sharing_count": ATTRIBUTE_VALUE
    "_thread_sharing_lock": ATTRIBUTE_VALUE
    "alias": ATTRIBUTE_VALUE
    "autocommit": ATTRIBUTE_VALUE
    "client": ATTRIBUTE_VALUE
    "close_at": ATTRIBUTE_VALUE
    "closed_in_transaction": ATTRIBUTE_VALUE
    "commit_on_exit": ATTRIBUTE_VALUE
    "connection": ATTRIBUTE_VALUE
    "creation": ATTRIBUTE_VALUE
    "errors_occurred": ATTRIBUTE_VALUE
    "execute_wrappers": ATTRIBUTE_VALUE
    "features": ATTRIBUTE_VALUE
    "force_debug_cursor": ATTRIBUTE_VALUE
    "in_atomic_block": ATTRIBUTE_VALUE
    "introspection": ATTRIBUTE_VALUE
    "isolation_level": ATTRIBUTE_VALUE
    "needs_rollback": ATTRIBUTE_VALUE
    "ops": ATTRIBUTE_VALUE
    "queries_log": ATTRIBUTE_VALUE
    "run_commit_hooks_on_set_autocommit_on": ATTRIBUTE_VALUE
    "run_on_commit": ATTRIBUTE_VALUE
    "savepoint_ids": ATTRIBUTE_VALUE
    "savepoint_state": ATTRIBUTE_VALUE
    "settings_dict": ATTRIBUTE_VALUE
        "ATOMIC_REQUESTS": ATTRIBUTE_VALUE
        "AUTOCOMMIT": ATTRIBUTE_VALUE
        "CONN_MAX_AGE": ATTRIBUTE_VALUE
        "ENGINE": ATTRIBUTE_VALUE
        "HOST": ATTRIBUTE_VALUE
        "NAME": ATTRIBUTE_VALUE
        "OPTIONS": ATTRIBUTE_VALUE
        "PASSWORD": ATTRIBUTE_VALUE
        "PORT": ATTRIBUTE_VALUE
        "TEST": ATTRIBUTE_VALUE
            "CHARSET": ATTRIBUTE_VALUE
            "COLLATION": ATTRIBUTE_VALUE
            "MIRROR": ATTRIBUTE_VALUE
            "NAME": ATTRIBUTE_VALUE
        },
        "TIME_ZONE": ATTRIBUTE_VALUE
        "USER": ATTRIBUTE_VALUE
    },
    "timezone": ATTRIBUTE_VALUE
    "timezone_name": ATTRIBUTE_VALUE
    "validation": ATTRIBUTE_VALUE
    "wrap_database_errors": ATTRIBUTE_VALUE

以及使用 dir() 的属性

print(json.dumps(dir(db_con), indent=4, sort_keys=True, default=str))
[
    "Database",
    "SchemaEditorClass",
    "__class__",
    "__delattr__",
    "__dict__",
    "__dir__",
    "__doc__",
    "__eq__",
    "__format__",
    "__ge__",
    "__getattribute__",
    "__gt__",
    "__hash__",
    "__init__",
    "__init_subclass__",
    "__le__",
    "__lt__",
    "__module__",
    "__ne__",
    "__new__",
    "__reduce__",
    "__reduce_ex__",
    "__repr__",
    "__setattr__",
    "__sizeof__",
    "__str__",
    "__subclasshook__",
    "__weakref__",
    "_close",
    "_commit",
    "_cursor",
    "_named_cursor_idx",
    "_nodb_connection",
    "_prepare_cursor",
    "_rollback",
    "_savepoint",
    "_savepoint_allowed",
    "_savepoint_commit",
    "_savepoint_rollback",
    "_set_autocommit",
    "_thread_ident",
    "_thread_sharing_count",
    "_thread_sharing_lock",
    "alias",
    "allow_thread_sharing",
    "autocommit",
    "check_constraints",
    "check_settings",
    "chunked_cursor",
    "clean_savepoints",
    "client",
    "client_class",
    "close",
    "close_at",
    "close_if_unusable_or_obsolete",
    "closed_in_transaction",
    "commit",
    "commit_on_exit",
    "connect",
    "connection",
    "constraint_checks_disabled",
    "copy",
    "create_cursor",
    "creation",
    "creation_class",
    "cursor",
    "data_type_check_constraints",
    "data_types",
    "data_types_suffix",
    "dec_thread_sharing",
    "disable_constraint_checking",
    "display_name",
    "enable_constraint_checking",
    "ensure_connection",
    "ensure_timezone",
    "errors_occurred",
    "execute_wrapper",
    "execute_wrappers",
    "features",
    "features_class",
    "force_debug_cursor",
    "get_autocommit",
    "get_connection_params",
    "get_new_connection",
    "get_rollback",
    "in_atomic_block",
    "inc_thread_sharing",
    "init_connection_state",
    "introspection",
    "introspection_class",
    "is_usable",
    "isolation_level",
    "make_cursor",
    "make_debug_cursor",
    "needs_rollback",
    "on_commit",
    "operators",
    "ops",
    "ops_class",
    "pattern_esc",
    "pattern_ops",
    "pg_version",
    "prepare_database",
    "queries",
    "queries_limit",
    "queries_log",
    "queries_logged",
    "rollback",
    "run_and_clear_commit_hooks",
    "run_commit_hooks_on_set_autocommit_on",
    "run_on_commit",
    "savepoint",
    "savepoint_commit",
    "savepoint_ids",
    "savepoint_rollback",
    "savepoint_state",
    "schema_editor",
    "set_autocommit",
    "set_rollback",
    "settings_dict",
    "temporary_connection",
    "timezone",
    "timezone_name",
    "validate_no_atomic_block",
    "validate_no_broken_transaction",
    "validate_thread_sharing",
    "validation",
    "validation_class",
    "vendor",
    "wrap_database_errors"
]

我想使用 db_con['queries'] 来获取查询列表,但它未显示在 __dict__ 中,如果不知道 db_con['queries'] 存在,我可能会感到困惑,我是通过dir()

我找到的第三方解决方案:

我正在使用runserver_plus

python manage.py runserver_plus

Runserver_plus: Django-extensions includes a management command (runserver_plus) to start the Werkzeug interactive debugger with your project

Werkzeug: Werkzeug is a WSGI utility library for Python. Beyond others, it includes an interactive debugger - what this means is that when your python application throws an exception, Werkzeug will display the exception stacktrace in the browser (that’s not a big deal) and allow you to write python commands interactively wherever you want in that stacktrace (that’s the important stuff).

无论我想检查一个变量,我都会在之后添加 a=+1 以便它抛出异常 (UnboundLocalError: local variable 'a' referenced before assignment) 然后我可以在那里打开一个交互式控制台并使用 [=31= 检查对象属性] 显示所有属性。目前我使用它检查属性。

我想使用常规 python 方式来完成。

obj.__dict__ 是实例属性的存储(对于基于 dict 的 classes - 有些是基于插槽的,有些(C 编码的)只是,嗯,它们是什么 xD)。其他 "properties"(注意:在 Python 中,'property' 也是为计算属性提供通用支持的内置 class 的名称),您会发现使用 dir(obj) 属于 class 或其父项之一,因此它们显然不在 obj.__dict__ 中。

此外,FWIW,dir() 不一定列出对象属性的 所有 :

Help on built-in function dir in module builtin:

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.

如果你真的想检索所有属性,你可以使用[getattr][1](obj, name[, default])即:

everything = {k: getattr(obj, k) for k in dir(obj)}

但是 你将无法将其序列化为 json - 函数/方法、属性(Python 内置类型...) , classes(对象的 class)和很多其他东西不是 json-serializable(你将如何序列化一个函数?)

此外,您可能有 classes 实现 __getattr__(通常用于动态委托),因此即使使用 dir 也可能存在可访问的属性(在一般意义上)在您的对象上,但无法通过检查列出。

So what is the best way to get a full dictionary of a attibutes of an object so that i can pretty print

我想到了 inspect.getmembers(obj) 之类的东西。但是您可能会发现 obj.__dict__ 通常已经是最有用的部分了。

and see using json.dumps

嗯,比照上面...

I want to use db_con['queries'] to get a the list of queries but its not shown in dict and can be confusing if dont know db_con['queries'] exists which i came to know through dir()

如果你在 python shell, you can just use the builtin help system.