Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #108085
| From | Dan Strohl <D.Strohl@F5.com> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | RE: Need help understanding list structure |
| Date | 2016-05-03 16:52 +0000 |
| Message-ID | <mailman.353.1462294382.32212.python-list@python.org> (permalink) |
| References | (6 earlier) <b93a85c8-042a-4c1f-800e-68a02dac3b78@googlegroups.com> <CAPTjJmoRxJSqAJNmWvxz9oNi294s=weAtM7yXC86OMYqq4jtjA@mail.gmail.com> <mailman.346.1462283241.32212.python-list@python.org> <db6035ff-7d16-4b21-92c8-c520a9b66230@googlegroups.com> <d70720619f3142b9876e7d43507cd95f@seaexchmbx03.olympus.F5Net.com> |
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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Need help understanding list structure moa47401@gmail.com - 2016-05-02 14:30 -0700
Re: Need help understanding list structure Erik <python@lucidity.plus.com> - 2016-05-02 22:48 +0100
Re: Need help understanding list structure moa47401@gmail.com - 2016-05-02 15:33 -0700
Re: Need help understanding list structure Michael Torrie <torriem@gmail.com> - 2016-05-02 17:25 -0600
Re: Need help understanding list structure Ben Finney <ben+python@benfinney.id.au> - 2016-05-03 09:43 +1000
Re: Need help understanding list structure moa47401@gmail.com - 2016-05-03 06:21 -0700
Re: Need help understanding list structure Chris Angelico <rosuav@gmail.com> - 2016-05-03 23:47 +1000
Re: Need help understanding list structure moa47401@gmail.com - 2016-05-03 09:01 -0700
RE: Need help understanding list structure Dan Strohl <D.Strohl@F5.com> - 2016-05-03 16:52 +0000
Re: Need help understanding list structure moa47401@gmail.com - 2016-05-03 10:31 -0700
RE: Need help understanding list structure Dan Strohl <D.Strohl@F5.com> - 2016-05-03 17:54 +0000
Use __repr__ to show the programmer's representation (was: Need help understanding list structure) Ben Finney <ben+python@benfinney.id.au> - 2016-05-04 04:14 +1000
RE: Use __repr__ to show the programmer's representation (was: Need help understanding list structure) Dan Strohl <D.Strohl@F5.com> - 2016-05-03 18:35 +0000
Re: Use __repr__ to show the programmer's representation (was: Need help understanding list structure) moa47401@gmail.com - 2016-05-03 12:24 -0700
Re: Use __repr__ to show the programmer's representation (was: Need help understanding list structure) Random832 <random832@fastmail.com> - 2016-05-03 15:37 -0400
Re: Need help understanding list structure MRAB <python@mrabarnett.plus.com> - 2016-05-03 20:57 +0100
Re: Use __repr__ to show the programmer's representation (was: Need help understanding list structure) Chris Angelico <rosuav@gmail.com> - 2016-05-04 09:40 +1000
csiph-web