python - 如何在 Python 中枚举对象的属性?

标签 python reflection properties

在 C# 中,我们通过反射来实现。在 Javascript 中很简单:

for(var propertyName in objectName)
    var currentPropertyValue = objectName[propertyName];

如何在 Python 中做到这一点?

最佳答案

for property, value in vars(theObject).items():
    print(property, ":", value)

请注意,在极少数情况下存在 __slots__ 属性,此类类通常没有 __dict__

关于python - 如何在 Python 中枚举对象的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1251692/

相关文章:

python - 使用 Python 和 Pandas 根据 Dataframe 内容重命名文件

c# - 如何将运行时已知的类型转换为给定类型的列表?

c# - 使用第三方库(带反射)较旧 .NET 版本的 .NET 应用程序

c# - 将变量绑定(bind)到列表

python - 从所有其他行中减去每一行并在 python 中查看为矩阵

python - 如何有条件地选择numpy数组中的元素

python - Django表单错误: 'QueryDict' object has no attribute 'method' when POST assigned

java - 途中加载类

c# - C# 中的链接属性和意外结果

java - Maven Profile 从 Properties 文件读取数据