Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Dan Strohl Newsgroups: comp.lang.python Subject: RE: Need help understanding list structure Date: Tue, 3 May 2016 16:52:53 +0000 Lines: 86 Message-ID: References: <5727CB31.5060309@lucidity.plus.com> <85mvo8gh57.fsf@benfinney.id.au> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de nR6QeYzS14u+1QP3SpJE4g+EGQB75m1lUoHU3XfJy47g== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'differently': 0.07; 'list?': 0.07; 'matches': 0.07; 'objects,': 0.07; 'subject:help': 0.07; '(string,': 0.09; 'behave': 0.09; 'below).': 0.09; 'defined,': 0.09; 'integer,': 0.09; 'integers': 0.09; 'list).': 0.09; 'methods,': 0.09; 'url:github': 0.09; 'stored': 0.10; 'python': 0.10; 'output': 0.13; 'do,': 0.15; '(assuming': 0.16; 'comments:': 0.16; 'confusion': 0.16; 'helps.': 0.16; 'iterating': 0.16; 'objects?': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'repr()': 0.16; 'str()': 0.16; 'string': 0.17; 'element': 0.18; 'integer': 0.18; 'library,': 0.18; 'library': 0.20; '(see': 0.20; 'to:name:python-list@python.org': 0.20; 'fix': 0.21; 'defined': 0.23; 'passing': 0.23; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'subject:list': 0.26; 'object,': 0.27; 'function': 0.28; 'this.': 0.28; 'actual': 0.28; 'looks': 0.29; 'dan': 0.29; 'itself,': 0.29; 'received:192.168.10': 0.29; 'subject:skip:u 10': 0.29; 'objects': 0.29; 'print': 0.30; 'checks': 0.30; 'run': 0.33; 'url:python': 0.33; 'definition': 0.34; 'file': 0.34; 'add': 0.34; 'that,': 0.34; 'list': 0.34; 'so,': 0.35; 'could': 0.35; 'text': 0.35; 'returning': 0.35; 'something': 0.35; 'item': 0.35; 'but': 0.36; 'list,': 0.36; 'there': 0.36; 'url:org': 0.36; 'created': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'method': 0.37; 'charset:us-ascii': 0.37; 'list.': 0.37; 'skip:p 20': 0.38; 'copying': 0.38; 'end': 0.39; 'why': 0.39; 'data': 0.39; 'does': 0.39; 'received:192': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'questions': 0.40; 'url:3': 0.60; 'your': 0.60; 'determine': 0.61; 'hope': 0.61; 'entire': 0.61; 'subject:Need': 0.61; 'back': 0.62; 'different': 0.63; 'complete': 0.63; 'results': 0.66; 'talking': 0.67; 'risk': 0.68; 'actually,': 0.84; 'received:192.168.15': 0.84; 'url:functions': 0.84; 'url:master': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=f5.com; i=@f5.com; q=dns/txt; s=seattle; t=1462294381; x=1493830381; h=from:to:subject:date:message-id:references:in-reply-to: content-transfer-encoding:mime-version; bh=xsO5bI/ELCiEpwC/o9W9adHp25A2Tfse4BfBrvnaa9w=; b=VJx+caqVyjjd+kT9gkDXzdM9PLxtVCf9RVcipGwTgTnK95wmm5CTPv2O MHqsZc+7irnXy1j5FUJGXjU7j4rJPO7nbYxQgo9iPTR5zlntbRKijVx5/ Cid5gPOFhtQdJjSI1p7ya1GVW8pAfZ6g0i3MP3XQybd56NjsQA/ZBZwyL 4=; X-IronPort-AV: E=Sophos;i="5.24,573,1454976000"; d="scan'208";a="216162162" Thread-Topic: Need help understanding list structure Thread-Index: AQHRpLrBPF9rfFcj/0a0e1h53WUhmJ+mpKGA//+YH0qAABNsD4AA5erSgAB6agD//7HU6oAAAIeg In-Reply-To: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [192.168.15.239] X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: <5727CB31.5060309@lucidity.plus.com> <85mvo8gh57.fsf@benfinney.id.au> Xref: csiph.com comp.lang.python:108085 Take a look at the docs for=20 print() https://docs.python.org/3.5/library/functions.html#print=20 str() https://docs.python.org/3.5/library/stdtypes.html#str repr() https://docs.python.org/3.5/library/functions.html#repr=20 When you do "print(object)", python will run everything through str() and o= utput it. =20 Str() will try to return a string representation of the object, what actual= ly 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 confus= ion > 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 locat= ion 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 dependin= g 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 __r= epr__ for the RANGE OBJECT or LIST object, not the object itself, which app= arently 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 (ju= st like when you did print(myList[0]) )=20 > 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 fir= st object in the list (though, keep in mind that the object type could be d= ifferent 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) a= t the end of the file you can see they defined __str__() in the ELEMENT obj= ect, but there is no definition for __repr__(), which matches what we surm= ised above. If you want to fix it by editing the gedcom library, you could simply add a= line at the end like: __repr__() =3D __str__() Hope that helps. Dan Strohl