Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

what is the difference between name and _name?

1,872 views
Skip to first unread message

luofeiyu

unread,
Aug 20, 2014, 3:16:56 AM8/20/14
to pytho...@python.org
When i learn property in python , i was confused by somename and _somename,what is the difference between them?

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")


>>> bob._name
'Bob'
>>> pt=Person("peter")
>>> pt.name
fetch....
'peter'
>>> pt._name
'peter'
>>> pt.name="tom"
change...
>>> pt._name="tom"
>>> pt._name
'tom'
>>> pt.name
fetch....
'tom'

Ben Finney

unread,
Aug 20, 2014, 3:28:23 AM8/20/14
to pytho...@python.org
luofeiyu <elear...@gmail.com> writes:

> When i learn property in python , i was confused by somename and
> _somename,what is the difference between them?

Some attributes are exposed in the API for an object (a class, a module,
etc.). Those are effectively a promise from the author that the
attribute is supported for use. A name like ‘foo’ is part of the API.

Other attributes are details of the implementation only, and are
designed to be used only by the object itself or its close
collaborators.

Those attributes which are not published, are not part of the API, are
details of the implementation. Depending on them to remain useful is an
error, since they can change at any time; they are not part of the
promise made by the designer of the object.

The convention on Python is to name implementation-detail attributes
with a single leading underscore. A name like ‘_foo’ is not part of the
API and using it from the outside is a mistake.

You should already have completed the Python tutorial
<URL:https://docs.python.org/3/tutorial/> by now. This conventionis
covered there; you should work through the tutorial from beginning to
end in order to get a good grasp of Python basics.

--
\ “Are you pondering what I'm pondering?” “Umm, I think so, |
`\ Brain, but what if the chicken won't wear the nylons?” —_Pinky |
_o__) and The Brain_ |
Ben Finney

Steven D'Aprano

unread,
Aug 20, 2014, 4:49:04 AM8/20/14
to
On Wed, 20 Aug 2014 15:16:56 +0800, luofeiyu wrote:

> When i learn property in python , i was confused by somename and
> _somename,what is the difference between them?

One name starts with underscore, the other does not. In names, an
underscore is just a letter with no special meaning:

foo, bar, foo_bar, spam, spam2, spam_with_eggs ...

But Python has some naming conventions:

(1) Names with TWO underscores at the start and end of the name are
reserved for Python: __dict__ __add__ __str__ etc. all have special
meaning to Python.

(2) Names with ONE leading underscore are intended as "private" variables
or names, the caller should not use it.

So in this example:

> class Person(object):
> def __init__(self, name):
> self._name = name
[...]
> name = property(getName, setName, delName, "name property docs")

(1) __init__ is a "dunder" (Double UNDERscore) name, and is special to
Python. It is used for the initialiser method;

(2) _name is a single underscore "private" attribute, only the Person
class is supposed to use it; and

(3) name is the public attribute that other classes or functions are
permitted to use.


Unless the documentation for the class or library says different, you
should ignore any _single underscore methods and attributes. They are
private, and subject to change without warning.



--
Steven
Message has been deleted
Message has been deleted
Message has been deleted

luofeiyu

unread,
Aug 20, 2014, 10:20:00 PM8/20/14
to Steven D'Aprano, pytho...@python.org
one more question,how can i get the doc info?

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")

Person("bob").name.doc
Person("bob").name.__doc__
Person("bob")._name.doc
Person("bob")._name.__doc__

all the expressions can not get the info "name property docs" ?how can i
get it?

Chris Angelico

unread,
Aug 20, 2014, 11:10:48 PM8/20/14
to pytho...@python.org
On Thu, Aug 21, 2014 at 12:20 PM, luofeiyu <elear...@gmail.com> wrote:
>
> all the expressions can not get the info "name property docs" ?how can i get
> it?

>>> Person.name.__doc__
'name property docs'

It's an attribute of the property, not of the result.

ChrisA

luofeiyu

unread,
Aug 22, 2014, 2:37:08 AM8/22/14
to Steven D'Aprano, pytho...@python.org
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.
>
>
>

0 new messages