I am working on a Python script that digs through a directory full of text files (Dungeon Crawl morgue files, if you’re wondering) and extracts and various values. When trying to do this the OOP way I have had some problems.
Here is the GameSummary class which takes in game_record_list – this is a List of each individual game records from which I can get the integer value record.gold.
class GameSummary:
def __init__(self, game_record_list):
self.game_record_list = game_record_list
self.gold_summary = self.gold_report()
def gold_total(self):
total_gold = 0
for record in self.game_record_list:
total_gold += record.gold
return total_gold
def gold_report(self):
report = "Total gold acquired: " + str(self.gold_total)
return report
Later I instantiate GameSummary as master_summary and try to write the string returned from gold_report into a log file using:
log_file.write(master_summary.gold_summary)
What ends up being written to my text file, however, is:
“Total gold acquired: bound method GameSummary.gold_total of <main.GameSummary instance at 0x02262FD0>”
Why isn’t this method returning a string? Is the issue in gold_total or gold_report?
should be
Alternatively,
gold_totalcould be made a property so it could be accessed like a field:Then the first way would work fine.