Need help understanding list structure

Dan Strohl D.Strohl at F5.com
Tue May 3 12:52:53 EDT 2016


Take a look at the docs for 
print() https://docs.python.org/3.5/library/functions.html#print 
str() https://docs.python.org/3.5/library/stdtypes.html#str
repr() https://docs.python.org/3.5/library/functions.html#repr 

When you do "print(object)", python will run everything through str() and output it.  

Str() will try to return a string representation of the object, what actually comes back will depend on how the objects author defined it.  If object.__str__() has been defined, it will use that, if __str__() is not defined, it will use object.__repr__().  If object.__repr__() has not been defined, it will something that looks like "object_name object at xxxxxxx".

So, as to your specific questions / comments:

> At the risk of coming across as a complete dunder-head, I think my confusion
> has to do with the type of data the library returns in the list. Any kind of text
> or integer list I manually create, doesn't do this.
Actually, they do, but strings and integers have well defined __str__ and __repr__ methods, and behave pretty well.

So... think about what is actually being passed to the print() function in each case:

> print(type(myList))
Passing the string object of the results of type(myList)

> print(len(myList))
Passing the integer object returning from len(myList)

> print(myList[0])
Passing the gedcom ELEMENT object found at location 0 in the list

> print(myList[0:29])
Passing a list object created by copying the items from location 0 to location 29 in the original list

> print(myList)
Passing the entire list object

> for x in myList:
>     print(x)
Passing the individual gedcom ELEMENT objects from the list.

So, in each of these cases, you are passing different types of objects, and each one (string, integer, list, gedcom)  will behave differently depending on how it is coded.

> Why does printing a single item print the actual text of the object?
If you are printing a single item (print(myList[0]), you are printing the __str__ or __repr__ for the object stored at that location.

> Why does printing a range print the "representations" of the objects?
If you are printing a range of objects, you are printing the __str__ or __repr__ for the RANGE OBJECT or LIST object, not the object itself, which apparently only checks for a __repr__() method in the contained gedcom ELEMENT objects, which is not defined (see below).

> Why does iterating over the list print the actual text of the objects?
When iterating over the list, you are printing the specific items again (just like when you did print(myList[0])  ) 

> How can I determine what type of data is in the list?
Try print(type(myList[0])), which will give you the type of data in the first object in the list (though, keep in mind that the object type could be different in each item in the list).


If we look at the gedcom library (assuming you are talking about this one: https://github.com/madprime/python-gedcom/blob/master/gedcom/__init__.py) at the end of the file you can see they defined __str__() in the ELEMENT object,  but there is no definition for __repr__(), which matches what we surmised above.

If you want to fix it by editing the gedcom library, you could simply add a line at the end like:
__repr__() = __str__()


Hope that helps.

Dan Strohl




More information about the Python-list mailing list