在 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/