SquidyEmerald SquidyEmerald - 2 months ago 15
Python Question

Dictionaries and Classes

I have this code:

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.")
}


And I want to do something like this:

for item in WEAPONS:
print(self.Name)


How would I go about doing it in Python 3?

Answer Source

Just iterate over the values:

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