what is the difference between name and _name?

Ben Finney ben+python at benfinney.id.au
Wed Aug 20 03:28:23 EDT 2014


luofeiyu <elearn2014 at 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




More information about the Python-list mailing list