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 17:54:38 +0000 Lines: 63 Message-ID: References: <5727CB31.5060309@lucidity.plus.com> <85mvo8gh57.fsf@benfinney.id.au> <221dcc70-39d8-4c9c-8827-8e0bc1ec1fda@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de laO85K3AgtdRODehz+Ut/wLvKINZ2Sd5TOqC+tS96E1g== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"""': 0.05; '(even': 0.05; 'difference,': 0.07; 'formatting': 0.07; 'properly.': 0.07; 'subject:help': 0.07; 'called.': 0.09; 'repr': 0.09; 'whatever.': 0.09; 'python': 0.10; 'def': 0.13; '"":': 0.16; 'dog': 0.16; 'examples:': 0.16; 'mylist': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'str()': 0.16; 'thought.': 0.16; 'string': 0.17; 'duplicate': 0.18; 'element': 0.18; "shouldn't": 0.18; 'tells': 0.18; '(in': 0.18; 'library': 0.20; 'to:name :python-list@python.org': 0.20; 'to:2**1': 0.21; 'header:In-Reply- To:1': 0.24; "doesn't": 0.26; 'subject:list': 0.26; '(which': 0.26; 'possibility': 0.27; 'looks': 0.29; 'end,': 0.29; 'received:192.168.10': 0.29; 'subject:skip:u 10': 0.29; 'read,': 0.29; 'allows': 0.30; 'print': 0.30; 'code': 0.30; 'help!': 0.30; 'guess': 0.31; 'probably': 0.31; 'option': 0.31; 'skip:_ 10': 0.32; 'point': 0.33; 'michael': 0.33; 'case,': 0.34; 'could': 0.35; 'easiest': 0.35; 'eric': 0.35; 'something': 0.35; "isn't": 0.35; 'sometimes': 0.35; 'but': 0.36; 'instead': 0.36; 'needed': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'really': 0.37; 'two': 0.37; 'method': 0.37; 'thanks': 0.37; 'charset:us-ascii': 0.37; 'doing': 0.38; 'someone': 0.38; 'end': 0.39; 'format': 0.39; 'enough': 0.39; 'takes': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'some': 0.40; 'save': 0.60; 'your': 0.60; 'subject:Need': 0.61; 'matter': 0.63; 'above,': 0.63; 'food': 0.64; 'here': 0.66; 'received:192.168.15': 0.84; 'glad': 0.87; 'glance': 0.91; 'spell': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=f5.com; i=@f5.com; q=dns/txt; s=seattle; t=1462298082; x=1493834082; h=from:to:subject:date:message-id:references:in-reply-to: content-transfer-encoding:mime-version; bh=MvD59m4Qv0qBcPyEsHcxv7zhvIBTKVML4MGLuECZxoM=; b=O+Va44I2mzhrwCyRupmnKMIAk6x1TaI9rVqBr6Xc5tsJS8ZqqHS1NVfI xgrnytg1m2O4Z343YYJm0SWOcTBvJmvx8HlSfl0/r5ZME7mFxCKDWXqcr +OZC3xPDrvH5JkPeEauxso3tm9nyzTBEcXu529U/jbOSN4vP089D72ym6 8=; X-IronPort-AV: E=Sophos;i="5.24,573,1454976000"; d="scan'208";a="216176928" Thread-Topic: Need help understanding list structure Thread-Index: AQHRpLrBPF9rfFcj/0a0e1h53WUhmJ+mpKGA//+YH0qAABNsD4AA5erSgAB6agD//7HU6oAAAIeggAAYq5aAAADgwA== In-Reply-To: <221dcc70-39d8-4c9c-8827-8e0bc1ec1fda@googlegroups.com> 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> <221dcc70-39d8-4c9c-8827-8e0bc1ec1fda@googlegroups.com> Xref: csiph.com comp.lang.python:108090 > I added a __repr__ method at the end of the gedcom library like so: >=20 > def __repr__(self): > """ Format this element as its original string """ > result =3D repr(self.level()) > if self.pointer() !=3D "": > result +=3D ' ' + self.pointer() > result +=3D ' ' + self.tag() > if self.value() !=3D "": > result +=3D ' ' + self.value() > return result >=20 > and now I can print myList properly. >=20 > Eric and Michael also mentioned repr above, but I guess I needed someone > to spell it out for me. Thanks for taking the time to put it in terms an = old dog > could understand. >=20 Glad to help! (being an old dog myself, I know the feeling!) One other point for you, if your "__repr__(self)" code is the same as the "= __str__(self)" code (which it looks like it is, at a glance at least), you = can instead reference the __str__ method and save having a duplicate code b= lock... some examples: =3D=3D=3D=3D=3D=3D=3D=3D=3D Option 1: This is the easiest to read (IMHO) and allows for the possibilit= y that str() is doing something here like formatting or whatever. (in this= case it shouldn't be though). However, to call this actually is taking mu= ltiple steps (calling object.__repr__, whch calls str(), which calls object= .__str__(). ) def __repr__(self): return str(self) =3D=3D=3D=3D=3D=3D=3D=3D=3D Option 2: this isn't hard to read, and just takes two steps (calling object= .__repr__(), which calls object.__str__(). def __repr__(self): return self.__str__() =3D=3D=3D=3D=3D=3D=3D=3D Option 3: it's not that this is hard to read, but since it doesn't follow = the standard "def blah(self):" pattern, sometimes I overlook these in the c= ode (even when I put them there). This however is the shortest since it re= ally just tells the object to return object.__str__() if either object.__re= pr__() OR object.__str__() is called. __repr__ =3D __str__ This probably doesn't matter much in this case, since it probably isn't cal= led that much in normal use (though there are always exceptions), and in th= e end, Python is fast enough that unless you really need to slice off a few= milliseconds, you will never notice the difference, but just some food for= thought.