打印子类和正确访问其信息的问题
Problems printing a subclass and having access to its information properly
这里有这个超级class:
class Item:
def __init__(self, name, desc, usable, value):
self.name = name
self.desc = desc
self.usable = usable
self.value = value
def __str__(self):
return "{}\n=====\n{}\nValue: {}\n".format(self.name, self.desc, self.usable, self.value)
还有这个子class(以及其他):
class HPot(Item):
def __init__(self):
super().__init__(desc="Heals for 5 HP.",
name="Health Potion",
usable=0,
value=3)
IHpot = HPot
它位于名为 Inv
:
的列表中
Inv = [IHpot]
而当我 print(Inv)
时,显然会发生这种情况:
[<class 'items.HPot'>]
所以我做了这个代码:
def openinv():
print("Your inventory:")
print(', '.join(Inv))
print("----------")
# [...]
然后发生这种情况:
Your inventory:
Traceback (most recent call last):
File "C:/Users/Bernardo/PycharmProjects/EndlessDungeon/inventory.py", line
29, in <module>
openinv()
File "C:/Users/Bernardo/PycharmProjects/EndlessDungeon/inventory.py", line 18, in openinv
print(', '.join(Inv))
TypeError: sequence item 0: expected str instance, type found
Process finished with exit code 1
我认为 return "{}\n=====\n{}\nValue: {}\n".format(self.name, self.desc, self.usable, self.value)
会有所帮助(它在 Item
superclass 中),但我尝试或在线搜索的任何内容都没有帮助我。我想像 Health Potion
一样打印它,并且仍然能够访问 class' name, desc, usable,
等。我该怎么做?
您从未创建子class Hpot
的实例。在 Python 中,一切都是对象,包括 classes。所以当你写行
IHpot = HPot
您没有创建 HPot 实例,而是为 class 本身创建了第二个名称。
要创建实例,您需要像调用函数一样调用 class。
IHpot = HPot()
这行代码创建了一个 HPot 实例,它具有您所说的所有这些属性。
你还有第二个问题。 IHpot 有一个 __str__
方法,但它不是一个 str。 join 函数需要一个字符串列表,而不是可以转换为字符串的对象列表。您必须自己转换列表,例如使用列表理解:
InvAsStrings = [str(a) for a in Inv]
您的库存包含 classes,但您似乎希望它包含那些 classes 的实例。
而不是:
IHPot = HPot
做:
IHPot = HPot()
然后对于 openinv
,您可以将每个 Item
实例变成 str
,如下所示:
def openinv():
print("Your inventory:")
print(', '.join([str(i) for i in Inv]))
print("----------")
您必须实例化 Item
class 才能使用实例方法(第一个参数为 self
的方法)。如果您需要更多信息,请搜索 "python class vs instance methods"。
这里有这个超级class:
class Item:
def __init__(self, name, desc, usable, value):
self.name = name
self.desc = desc
self.usable = usable
self.value = value
def __str__(self):
return "{}\n=====\n{}\nValue: {}\n".format(self.name, self.desc, self.usable, self.value)
还有这个子class(以及其他):
class HPot(Item):
def __init__(self):
super().__init__(desc="Heals for 5 HP.",
name="Health Potion",
usable=0,
value=3)
IHpot = HPot
它位于名为 Inv
:
Inv = [IHpot]
而当我 print(Inv)
时,显然会发生这种情况:
[<class 'items.HPot'>]
所以我做了这个代码:
def openinv():
print("Your inventory:")
print(', '.join(Inv))
print("----------")
# [...]
然后发生这种情况:
Your inventory:
Traceback (most recent call last):
File "C:/Users/Bernardo/PycharmProjects/EndlessDungeon/inventory.py", line
29, in <module>
openinv()
File "C:/Users/Bernardo/PycharmProjects/EndlessDungeon/inventory.py", line 18, in openinv
print(', '.join(Inv))
TypeError: sequence item 0: expected str instance, type found
Process finished with exit code 1
我认为 return "{}\n=====\n{}\nValue: {}\n".format(self.name, self.desc, self.usable, self.value)
会有所帮助(它在 Item
superclass 中),但我尝试或在线搜索的任何内容都没有帮助我。我想像 Health Potion
一样打印它,并且仍然能够访问 class' name, desc, usable,
等。我该怎么做?
您从未创建子class Hpot
的实例。在 Python 中,一切都是对象,包括 classes。所以当你写行
IHpot = HPot
您没有创建 HPot 实例,而是为 class 本身创建了第二个名称。
要创建实例,您需要像调用函数一样调用 class。
IHpot = HPot()
这行代码创建了一个 HPot 实例,它具有您所说的所有这些属性。
你还有第二个问题。 IHpot 有一个 __str__
方法,但它不是一个 str。 join 函数需要一个字符串列表,而不是可以转换为字符串的对象列表。您必须自己转换列表,例如使用列表理解:
InvAsStrings = [str(a) for a in Inv]
您的库存包含 classes,但您似乎希望它包含那些 classes 的实例。
而不是:
IHPot = HPot
做:
IHPot = HPot()
然后对于 openinv
,您可以将每个 Item
实例变成 str
,如下所示:
def openinv():
print("Your inventory:")
print(', '.join([str(i) for i in Inv]))
print("----------")
您必须实例化 Item
class 才能使用实例方法(第一个参数为 self
的方法)。如果您需要更多信息,请搜索 "python class vs instance methods"。