Need help understanding list structure

Erik python at lucidity.plus.com
Mon May 2 17:48:33 EDT 2016


On 02/05/16 22:30, moa47401 at gmail.com wrote:
> Can someone help me understand why or under what circumstances a list
> shows pointers instead of the text data?

When Python's "print" statement/function is invoked, it will print the 
textual representation of the object according to its class's __str__ or
__repr__ method. That is, the print function prints out whatever text
the class says it should.

For classes which don't implement a __str__ or __repr__ method, then
the text "<CLASS object at ADDRESS>" is used - where CLASS is the class
name and ADDRESS is the "memory pointer".

 > If I iterate over the list, I do get the actual text of each element
 > and am able to use it.
 >
 > Also, if I iterate over the list and place each element in a new list
 > using append, then each element in the new list is the text I expect
 > not memory pointers.

Look at the __iter__ method of the class of the object you are iterating 
over. I suspect that it returns string objects, not the objects that are 
in the list itself.

String objects have a __str__ or __repr__ method that represents them as 
the text, so that is what 'print' will output.

Hope that helps, E.



More information about the Python-list mailing list