1

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>]
wpercy
  • 9,636
  • 4
  • 33
  • 45
jKraut
  • 2,325
  • 6
  • 35
  • 48

1 Answers1

1

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]
Erik Godard
  • 5,930
  • 6
  • 30
  • 33