Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76777
| Date | 2014-08-22 14:37 +0800 |
|---|---|
| From | luofeiyu <elearn2014@gmail.com> |
| 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> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.13275.1408689455.18130.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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