Need help understanding list structure

moa47401 at gmail.com moa47401 at gmail.com
Mon May 2 18:33:25 EDT 2016


> 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.

Yes, that does help. You're right. The author of the library I'm using didn't implement either a __str__ or __repr__ method. Am I correct in assuming that parsing a large text file would be quicker returning pointers instead of strings? I've never run into this before.





More information about the Python-list mailing list