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


Groups > comp.lang.python > #76777

Re: what is the difference between name and _name?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <elearn2014@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'method.': 0.07; '__init__': 0.09; 'method,': 0.09; 'runs': 0.10; 'python': 0.11; 'def': 0.12; 'finds': 0.16; 'name):': 0.16; 'subject:between': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'fix': 0.17; 'header:User-Agent:1': 0.23; 'error': 0.23; 'looks': 0.24; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; '"",': 0.31; "d'aprano": 0.31; 'depth': 0.31; 'object.': 0.31; 'steven': 0.31; 'subject:what': 0.31; 'file': 0.32; 'class': 0.32; 'skip:_ 10': 0.34; 'subject:the': 0.34; 'received:google.com': 0.35; 'method': 0.36; 'subject:?': 0.36; 'error.': 0.37; 'received:10': 0.37; 'message-id:@gmail.com': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'according': 0.40; 'skip:x 10': 0.40; 'name': 0.63; 'maximum': 0.63; 'charset:windows-1252': 0.65; 'mistake': 0.91; 'exceeded': 0.97
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=ZIl8X3ofCpSxwxdRvzQw5BDnJhckFNsLdib3OwJV18E=; b=KzhnoimsjKt04TotTro2lPvMyHw9S29Xej6jOGwAVu1dfKgVjhE4dtbJL6WDC69XeY IM8iO2jIKvKIT0Ro6Ik5o/9O8D/9sOX6f/OmNAozJ5eU0ZH+1XMKJktMsNXIVGKLxTR0 9ZqKuQrhUpsLyVkFNkFHi08mTty+e9dOl4YIOBdFeFXc7WiydQ1w3aJuamQkXEnC/eRq 1YTZlGLwM6/jwkSTJt6dPBzjeZZG688ssVViSIfOc+J4c2q//ktfN6aRdsxPiR8R1YMF 0JL3XOmUZHfE6jgP1g41JIiDln+TonNJVJQq0aZQDYTd+63arD/zkkCwdXquB7eTYmUR RFUQ==
X-Received by 10.69.18.203 with SMTP id go11mr4042398pbd.50.1408689452185; Thu, 21 Aug 2014 23:37:32 -0700 (PDT)
Date Fri, 22 Aug 2014 14:37:08 +0800
From luofeiyu <elearn2014@gmail.com>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.0
MIME-Version 1.0
To Steven D'Aprano <steve+comp.lang.python@pearwood.info>, python-list@python.org
Subject Re: what is the difference between name and _name?
References <mailman.13190.1408519045.18130.python-list@python.org> <53f46100$0$29884$c3e8da3$5496439d@news.astraweb.com> <mailman.13201.1408534898.18130.python-list@python.org> <53f4be1f$0$29978$c3e8da3$5496439d@news.astraweb.com>
In-Reply-To <53f4be1f$0$29978$c3e8da3$5496439d@news.astraweb.com>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.13275.1408689455.18130.python-list@python.org> (permalink)
Lines 60
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1408689455 news.xs4all.nl 2915 [2001:888:2000:d::a6]:47090
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:76777

Show key headers only | View raw


I fix a mistake in Steven D'Aprano interpretation.

class Person(object):
      def __init__(self, name):
          self._name = name
      def getName(self):
          print('fetch....')
          return self._name
      def setName(self, value):
          print('change...')
          self._name = value
      def delName(self):
          print('remove....')
          del self._name
      _name = property(getName, setName, delName, "name property docs")


x=Person("peter")

It can not initinalize.
   File "<stdin>", line 9, in setName
   File "<stdin>", line 8, in setName
RuntimeError: maximum recursion depth exceeded while calling a Python 
object.

Steven D'Aprano interpretation:

10 Python finds the property _name
20 Python retrieves the getter, getName
30 Python runs the getName() method
40 which looks up self._name
50 go to 10

the right interpretation according to the error message:


10 python call __init__ method. self._name = name
20 python call setName method,
          print('change...')
          self._name = value
30 from self._name = value ,python call call setName method again

then we get a recursion error.
>> why i can not write  _name = property(getName, setName, delName, "name
>> property docs") ?
> Because you will have infinite recursion.
>
> When you look up instance._name:
>
> 10 Python finds the property _name
> 20 Python retrieves the getter, getName
> 30 Python runs the getName() method
> 40 which looks up self._name
> 50 go to 10
>
> and you get a recursion error.
>
>
>

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


Thread

what is the difference between name and _name? luofeiyu <elearn2014@gmail.com> - 2014-08-20 15:16 +0800
  Re: what is the difference between name and _name? Steven D'Aprano <steve@pearwood.info> - 2014-08-20 08:49 +0000
    Re: what is the difference between name and _name? luofeiyu <elearn2014@gmail.com> - 2014-08-20 19:41 +0800
      Re: what is the difference between name and _name? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-21 01:26 +1000
        Re: what is the difference between name and _name? luofeiyu <elearn2014@gmail.com> - 2014-08-21 10:20 +0800
        Re: what is the difference between name and _name? Chris Angelico <rosuav@gmail.com> - 2014-08-21 13:10 +1000
        Re: what is the difference between name and _name? luofeiyu <elearn2014@gmail.com> - 2014-08-22 14:37 +0800
    Re: what is the difference between name and _name? "Frank Millman" <frank@chagford.com> - 2014-08-20 14:18 +0200

csiph-web