Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #108090

RE: Need help understanding list structure

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Dan Strohl <D.Strohl@F5.com>
Newsgroups comp.lang.python
Subject RE: Need help understanding list structure
Date Tue, 3 May 2016 17:54:38 +0000
Lines 63
Message-ID <mailman.354.1462298084.32212.python-list@python.org> (permalink)
References <d6b0c524-f020-4726-b91a-517b2e8024ea@googlegroups.com> <5727CB31.5060309@lucidity.plus.com> <mailman.329.1462225716.32212.python-list@python.org> <ab5d3884-77ed-45d4-86ab-0333bf67dca1@googlegroups.com> <85mvo8gh57.fsf@benfinney.id.au> <mailman.332.1462232638.32212.python-list@python.org> <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> <mailman.353.1462294382.32212.python-list@python.org> <221dcc70-39d8-4c9c-8827-8e0bc1ec1fda@googlegroups.com> <cdb6b67026d64546825afea8576bf4f0@seaexchmbx03.olympus.F5Net.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 <prvs=924e6f62e=D.Strohl@f5.com>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <cdb6b67026d64546825afea8576bf4f0@seaexchmbx03.olympus.F5Net.com>
X-Mailman-Original-References <d6b0c524-f020-4726-b91a-517b2e8024ea@googlegroups.com> <5727CB31.5060309@lucidity.plus.com> <mailman.329.1462225716.32212.python-list@python.org> <ab5d3884-77ed-45d4-86ab-0333bf67dca1@googlegroups.com> <85mvo8gh57.fsf@benfinney.id.au> <mailman.332.1462232638.32212.python-list@python.org> <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> <mailman.353.1462294382.32212.python-list@python.org> <221dcc70-39d8-4c9c-8827-8e0bc1ec1fda@googlegroups.com>
Xref csiph.com comp.lang.python:108090

Show key headers only | View raw


> I added a __repr__ method at the end of the gedcom library like so:
> 
> def __repr__(self):
>         """ Format this element as its original string """
>         result = repr(self.level())
>         if self.pointer() != "":
>             result += ' ' + self.pointer()
>         result += ' ' + self.tag()
>         if self.value() != "":
>             result += ' ' + self.value()
>         return result
> 
> and now I can print myList properly.
> 
> 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.
> 

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 block...  some examples:

=========
Option 1:  This is the easiest to read (IMHO) and allows for the possibility 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 multiple steps (calling object.__repr__, whch calls str(), which calls object.__str__(). )

def __repr__(self):
    return str(self)

=========
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__()

========
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 code (even when I put them there).  This however is the shortest since it really just tells the object to return object.__str__() if either object.__repr__() OR object.__str__() is called.


__repr__ = __str__


This probably doesn't matter much in this case, since it probably isn't called that much in normal use (though there are always exceptions), and in the 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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