I am trying to emulate a Java toString method I learned a while back. I might be off base here and there may definitely be an easier way to do what I'm trying to do with a simple list.
What I really want to do is be able to append objects to a list, and then print specific traits about those objects from the list. I also want to keep track of the index of the object. Here is what I have so far, but when I print, I get no errors, and a blank line, nothing prints.
class Character_list():
def __init__(self, list):
self.list = []
def toString(self):
result = ""
for i in self.list:
result += self.list[i]
return result
def main():
x = Character_list([1, 2])
print(x.toString())
main()
self.list = [], there's nothing to be printed... You probably meantself.list = list.__str__method that you can write for yourself to customise how the objects are displayed when a string representation is needed.listas a variable name as it clashes with thelist()inbuilt function in Python. Change it to something else likechar_listetc