working with classes, inheritance, _str_ returns and a list

David D daviddschool at gmail.com
Sun Jan 15 14:58:16 EST 2017


I am creating a parent class and a child class.  I am inheriting from the parent with an additional attribute in the child class.  I am using __str__ to return the information.  When I run the code, it does exactly what I want, it returns the __str__ information.  This all works great. 

BUT

1) I want what is returned to be appended to a list (the list will be my database)
2) I append the information to the list that I created
3) Whenever I print the list, I get a memory location

So how do I take the information that is coming out of the child class (as a __str__ string), and keep it as a string so I can append it to the list?

pseudo code

allcars=[]

parent class()
   def init (self, model, wheels, doors)
      self.model= model etc

child class (parent)
   def init(self, model, wheels, doors, convertible)
       super(child, self).__init__(model, wheels, doors)
       self.convertible = convertible

   def __str__(self):
        return "model: " + self.model + etc

car1= child(model, wheels, doors, convertible)
print car1



Here is where it goes wrong for me


allcars.append(car1)

I am sure I am making a silly mistake in here somewhere...







More information about the Python-list mailing list