Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel




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)

>Solution :

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__

t = TreasureChest("What is the airspeed veloicity of an unladen swallow?", 42, 100)
print(t)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading