[英]print(object.method) not printing expected output?
I'm having a foray into OOP in python, 我正在尝试使用python进行OOP,
This project is creating some randomly generated RPG characters 该项目正在创建一些随机生成的RPG角色
The problem I have run into is I am creating a list of these randomly generated characters, and want to print out there stats. 我遇到的问题是我正在创建这些随机生成的字符的列表,并希望在那里打印统计数据。
Here is how the characters are randomly generated: 这是随机生成字符的方式:
def generateCharacters():
classes = ["B", "E", "W", "D", "K"]
choice = random.choice(classes)
if choice == "B":
return barbarian(70, 20, 50)
elif choice == "E":
return elf(30, 60, 10)
elif choice == "W":
return wizard(50, 70, 30)
elif choice == "D":
return dragon(90, 40, 50)
elif choice == "K":
return knight(60, 10, 60)
and here is the barbarian class, all the other classes are more or less identical: 这是野蛮阶级,所有其他阶级或多或少是相同的:
class barbarian(character):
def __init__(self, charPower, charSAttackPwr, charSpeed):
# Getting the properties from the inheritted character Base Class
character.__init__(self, "B", 100)
self.power = charPower
self.sAttackPwr = charSAttackPwr
self.speed = charSpeed
# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack
Power: %s, Speed: %s" % (self.name, self.type, self.health,
self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats
I've created a method called getStats, which using string concatenation to make a string that shows all the stats: 我创建了一个名为getStats的方法,该方法使用字符串连接来创建一个显示所有统计信息的字符串:
# Method for getting and returning all the stats of the character
def getStats(self):
# Creating a string to hold all the stats, using concatenation
stats = "Name: %s, Type: %s, Health: %s, Power: %s, Special Attack Power: %s, Speed: %s" % (self.name, self.type, self.health, self.power, self.sAttackPwr, self.speed)
# Returns stats to the the function that called
return stats
When I run the code, it calls main(), which in turn calls menu(): 当我运行代码时,它调用main(),而后者又调用menu():
def menu(gameChars):
print("Welcome to the RPG Character Simulator")
print("Here is your randomly generated team: ")
for x in gameChars:
print(x.getStats)
def main():
gameChars = []
for x in range(10):
y = generateCharacters()
gameChars.insert(x, y)
#z = generateCharacters()
menu(gameChars)
#print(z.getStats)
The output I was expecting from the print(x.getStats) would've been, using examples: 使用示例,我期望从print(x.getStats)获得的输出将是:
Name: bob, Type: barbarian, Health: 100, Power: 70, Special Attack Power: 20, Speed: 20
but instead, I get this: 但是,我得到了这个:
<bound method barbarian.getStats of <__main__.barbarian object at 0x000001F56A195668>>
What am I missing with this? 我想念什么? and how could I get the intended output? 以及如何获得预期的输出?
Thanks in advance for any help 预先感谢您的任何帮助
Replace this : 替换为:
print(x.getStats)
With this : 有了这个 :
print(x.getStats())
another version would be to use the @property
decorator: 另一个版本是使用@property
装饰器:
class Barbarian(Character):
@property
def getStats(self):
return 'Name: {.name}'.format(self)
which would allow: 这将允许:
bob = Barbarian('bob', …)
print(bob.getStats)
to work as you expect 像您期望的那样工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.