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


Groups > comp.lang.python > #108096

Re: Need help understanding list structure

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From MRAB <python@mrabarnett.plus.com>
Newsgroups comp.lang.python
Subject Re: Need help understanding list structure
Date Tue, 3 May 2016 20:57:12 +0100
Lines 57
Message-ID <mailman.360.1462305440.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> <1ca9650f-3f4f-f4b5-529d-c7d57e8a2171@mrabarnett.plus.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de sgTNX2d/sAY9gFZh27E2YgIaXUzAC8FAZHagGO0IPrLw==
Return-Path <python@mrabarnett.plus.com>
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; '"""': 0.05; '(even': 0.05; 'method.': 0.05; 'difference,': 0.07; 'formatting': 0.07; 'properly.': 0.07; 'subject:help': 0.07; 'called.': 0.09; 'falls': 0.09; 'repr': 0.09; 'whatever.': 0.09; 'python': 0.10; 'def': 0.13; '"":': 0.16; 'dog': 0.16; 'examples:': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'mylist': 0.16; 'received:192.168.1.4': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'str()': 0.16; 'thought.': 0.16; 'wrote:': 0.16; 'string': 0.17; 'duplicate': 0.18; 'element': 0.18; "shouldn't": 0.18; 'tells': 0.18; '(in': 0.18; 'library': 0.20; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'subject:list': 0.26; '(which': 0.26; 'possibility': 0.27; 'looks': 0.29; 'dan': 0.29; 'end,': 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; 'doing': 0.38; 'delete': 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; 'back': 0.62; 'matter': 0.63; 'above,': 0.63; 'food': 0.64; 'python-list': 0.66; 'here': 0.66; 'glad': 0.87; 'glance': 0.91; 'spell': 0.91
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=K//fZHiI c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IkcTkHD0fZMA:10 a=_sXNDiKYjg4MNy601lkA:9 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett@:2500
User-Agent Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.0
In-Reply-To <cdb6b67026d64546825afea8576bf4f0@seaexchmbx03.olympus.F5Net.com>
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 <1ca9650f-3f4f-f4b5-529d-c7d57e8a2171@mrabarnett.plus.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> <cdb6b67026d64546825afea8576bf4f0@seaexchmbx03.olympus.F5Net.com>
Xref csiph.com comp.lang.python:108096

Show key headers only | View raw


On 2016-05-03 18:54, Dan Strohl via Python-list wrote:
>
>> 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.
>
>
Option 4:

Delete the __str__ method.


If there's no __str__, Python falls back to __repr__.

If there's no __repr__, Python falls back to the <Something object at 
somewhere> format.

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