jKraut
jKraut

Reputation: 2487

Get Value of Instance List as a String in python

Working with this code, and my output is not the list of strings, but instances. Do I re-write my LineCount class to allow such behavior?

line_count =[]

class LineCount:
    def __init__(self, x):
        self.x = x

    def set_x(self, x):
        self.x = x

    def get_x(self):
        return self.x

# add a value
line_count.append(LineCount(1))

# print list - does not print our string values
print line_count
# [<__main__.LineCount instance at 0x1031d53f8>]

Upvotes: 1

Views: 463

Answers (1)

Erik Godard
Erik Godard

Reputation: 6030

You can change your print statement to call the get_x function for each element in the list using a list comprehesion.

print [y.get_x() for y in line_count]

Upvotes: 1

Related Questions