How to print the object itself instead of the location of the object python?

Question:

I am making a program that will read lines from a text file which contain the question, the answer and the points awarded which are on lines i, i+1 and i+2 respectively. The lines will be read and then saved in an array of type TreasureChest. But when I get print I get the output:


[<__main__.TreasureChest object at 0x7f89197c5470>, <__main__.TreasureChest object at 0x7f89197c54a8>, <__main__.TreasureChest object at 0x7f89197c5400>, <__main__.TreasureChest object at 0x7f89197c5438>, <__main__.TreasureChest object at 0x7f89197c57b8>, <__main__.TreasureChest object at 0x7f89197c5828>, <__main__.TreasureChest object at 0x7f89197c5860>, <__main__.TreasureChest object at 0x7f89197c5898>, <__main__.TreasureChest object at 0x7f89197c58d0>, <__main__.TreasureChest object at 0x7f89197c5908>, <__main__.TreasureChest object at 0x7f89197c5940>, <__main__.TreasureChest object at 0x7f89197c5978>, <__main__.TreasureChest object at 0x7f89197c59b0>, <__main__.TreasureChest object at 0x7f89197c59e8>, <__main__.TreasureChest object at 0x7f89197c5a20>]



I know this is a common problem on here about printing the string instead of the object location but none of the answers on here have worked or been useful in my case.

Here is the code:




import linecache


searchfile = open("TreasureChestData.txt", 'r')

class TreasureChest:

    def __init__(self, question, answer, points):

        self.question = question 
        self.answer = answer 
        self.points = points 



    

def read_data():

    searchfile = open("TreasureChestData.txt", 'r')
    arrayTreasure = []
    

    for i in range (1, 16):


        question = linecache.getline('TreasureChestData.txt', i)
        answer = linecache.getline('TreasureChestData.txt', (i+1))
        pointer = linecache.getline('TreasureChestData.txt', (i+2))

        arrayTreasure.append(TreasureChest(question, answer, pointer))

        i = i + 3

    print (len(arrayTreasure))

    return arrayTreasure

    searchfile.close()
    
data = read_data()
print (data)

Asked By: shahmir782

||

Answers:

All you are seeing is the default implementation of the special __str__ method. All you have to do is def __str__(self):, and have it return whatever string you want to be used instead.

Answered By: Tim Roberts

Define the class’s __repr__ (which controls how it appears in containers, among other places) and its __str__ (which controls how it appears when you print it). They can be the same, as shown here.

class TreasureChest:

    def __init__(self, question, answer, points):
        self.question = question 
        self.answer = answer 
        self.points = points 

    def __repr__(self):
        return f'{type(self).__name__}("{self.question}")'

    __str__ = __repr__   # not strictly necessary; see comment below

t = TreasureChest("What is the airspeed veloicity of an unladen swallow?", 42, 100)
print(t)
Answered By: kindall
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.