Gold is not an attribute on the Item class, no. It is a subclass, and a global name in its own right. You can import it from your items module:
>>> from items import Gold
>>> Gold
<class 'items.Gold'>
You cannot create an instance of it, because used the wrong name for the Item.__init__ method:
>>> from items import Item
>>> Item.__init__
<slot wrapper '__init__' of 'object' objects>
>>> Item.__init___
<function Item.__init___ at 0x1067be510>
>>> Item('a', 'b', 4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object() takes no parameters
Note that the method you created has three underscores in the name. If you fix that:
class Item():
def __init__(self, name, desc, val):
# ^ ^ 2 underscores on both sides
self.name = name
self.desc = desc
self.val = val
you can create instances of the Gold() class:
>>> Gold()
<items.Gold object at 0x1067cfb00>
>>> gold = Gold()
>>> print(gold.print_info())
Gold
==========
Golden coin.
Value: 5
Now, if you really wanted to create attributes on the Item class, you'll have to add those after you created the class:
class Item():
def __init___(self, name, desc, val):
self.name = name
self.desc = desc
self.val = val
def print_info(self):
return '{}\n==========\n{}\n\nValue: {}'.format(self.name, self.desc, self.val)
Item.gold = Item('Gold', 'Golden coin.', 5)
You don't need to create subclasses for that. You could use the enum module here though:
from enum import Enum
class Item(Enum):
Gold = 'Golden coin.', 5
Silver = 'Silver coin.', 1
def __init__(self, desc, val):
self.desc = desc
self.val = val
def print_info(self):
return '{}\n==========\n{}\n\nValue: {}'.format(self.name, self.desc, self.val)
Here Gold is an attribute of Item:
>>> Item
<enum 'Item'>
>>> Item.Gold
<Item.Gold: ('Golden coin.', 5)>
>>> print(Item.Gold.print_info())
Gold
==========
Golden coin.
Value: 5
>>> Item.Silver
<Item.Silver: ('Silver coin.', 1)>
print_infoItem.Gold?Goldis not an attribute ofItemsoItem.Golddoesn't make much sense. It's a subclass, it's a entirely differnt thing. What are you trying to achieve?