working with classes, inheritance, _str_ returns and a list
rosuav at gmail.com
Sun Jan 15 15:08:04 EST 2017
On Mon, Jan 16, 2017 at 6:58 AM, David D <daviddschool at gmail.com> wrote:
> 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.
> 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
Technically it's an identity, not a memory location, but yes, I know
what you mean here.
What's happening is that printing a list actually prints the *repr* of
an object, rather than its str. The easiest change is to rename your
__str__ function to __repr__; if you don't need them to be different,
define __repr__ and it'll be used for printing as well.
By the way, you'll find that Python 3 makes some things with class
definitions a bit easier. There are less traps to potentially fall
into. I strongly suggest using the latest Python (currently 3.6) or
something close to it (3.5 is in a lot of Linux distributions).
All the best!
More information about the Python-list