Skip to main content
100%

[获得所有变量/属性的名称和值] #python

✓ Published0🌍 Public
SShawn91
Last edited Jan 25, 2019
Created on Jan 25, 2019

This example demonstrates how to retrieve all variable names and their values in Python, both at the module level and for object attributes, using the built-in `vars()` function. It shows that calling `vars()` without arguments prints the current local symbol table, including special module attributes like `__name__` and `__package__` in addition to user-defined variables such as a dictionary `a`. The code then applies `vars()` to an instance of a custom `Info` class, outputting its attribute dictionary `{'name': 'John'}`. The example relies solely on Python’s standard library, explicitly using `vars()` to inspect namespaces.

AI-generated description

Similar vizzes