Need help understanding list structure

Michael Torrie torriem at gmail.com
Mon May 2 19:25:24 EDT 2016


On 05/02/2016 04:33 PM, moa47401 at gmail.com wrote:
> 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.

I'm not sure what you mean by "returning pointers." The list isn't
returning pointers. It's a list of *objects*.  To be specific, a list of
gedcom.Element objects, though they could be anything, including numbers
or strings.  If you refer to the source code where the Element class is
defined you can see what these objects contain. I suspect they contain a
lot more information than simply text.

Lists of objects is a common idiom in Python.  As you've discovered, if
you shallow copy a list, the new list will contain the exact same
objects.  In many cases, this does not matter.  For example a list of
numbers, which are immutable or unchangeable objects.  It doesn't matter
that the instances are shared, since the instances themselves will never
change.  If the objects are mutable, as they are in your case, a shallow
copy may not always be what you want.

As to your question.  A list never shows "pointers" as you say.  A list
always contains objects, and if you simply "print" the list, it will try
to show a representation of the list, using the objects' repr dunder
methods.  Some classes I have used have their repr methods print out
what the constructor would look like, if you were to construct the
object yourself.  This is very useful.  If I recall, this is what
BeautifulSoup objects do, which is incredibly useful.

In your case, as Erik said, the objects you are dealing with don't
provide repr dunder methods, so Python just lets you know they are
objects of a certain class, and what their ids are, which is helpful if
you're trying to determine if two objects are the same object.  These
are not "pointers" in the sense you're talking.  You'll get text if the
object prints text for you. This is true of any object you might store
in the list.

I hope this helps a bit.  Exploring from the interactive prompt as you
are doing is very useful, once you understand what it's saying to you.



More information about the Python-list mailing list