This question already has answers here:
Access custom object property while iterating over dictionary
(2个答案)
两年前关闭。
我有这个密码:
class weapon():
    def __init__(self, Name, Type, Description):
        self.Name=Name
        self.Type=Type
        self.Description=Description

WEAPONS = { "starterSword":weapon("Starter Sword", "Sword", "A short, stunt steel sword."),
        "basicSword":weapon("Basic Sword", "Sword", "A basic steel sword.")
        }

我想这样做:
for item in WEAPONS:
    print(self.Name)

在Python 3中,我该怎么做呢?

最佳答案

只需迭代values

for item in WEAPONS.values():
    print(item.Name)

10-01 06:48