字典和类

Squ*_*ald 2 python iteration dictionary python-3.x

我有这个代码:

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.")
        }
Run Code Online (Sandbox Code Playgroud)

我想做这样的事情:

for item in WEAPONS:
    print(self.Name)
Run Code Online (Sandbox Code Playgroud)

我将如何在Python 3中进行此操作?

MSe*_*ert 5

只需迭代:

for item in WEAPONS.values():
    print(item.Name)
Run Code Online (Sandbox Code Playgroud)