16

What convention is it?

class IndexedText(object):
    def __init__(self, stemmer, text):
        self._text = text
        self._stemmer = stemmer
        self._index = nltk.Index((self._stem(word), i) for (i, word) in enumerate(text))
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080
  • 1
    Possible duplicate of [What is the meaning of a single- and a double-underscore before an object name?](http://stackoverflow.com/questions/1301346/what-is-the-meaning-of-a-single-and-a-double-underscore-before-an-object-name) – xli Apr 19 '17 at 02:50

5 Answers5

19

The _ signals that these are private members. It's not enforced by the language in any way, since Python programmers are all "consenting adults".

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • so the underscore means "private instance variable", but python does not enforce it, right? It's just a convention. – TIMEX Jul 14 '11 at 22:51
  • 2
    Yes python doesnt force or complain if you do so(access from outside). And it is generally adopted by and for developers to find out difference between which variable is used by internal and which is to be refered from outside or as instance variable –  Jul 08 '15 at 11:51
  • However, I would prefer language to enforce since it doesnt make sense otherwise to allow access for internal variable from outside for no real reason –  Jul 08 '15 at 11:52
  • Is it really private or it is protected i.e. subclasses can access Parent._method() – Claudiu Creanga Oct 24 '19 at 09:48
13

According to PEP 8:

In addition, the following special forms using leading or trailing underscores are recognized (these can generally be combined with any case convention):

  • _single_leading_underscore: weak "internal use" indicator. E.g. from M import * does not import objects whose name starts with an underscore.

It doesn't actually refer to the use of a single underscore in a member of a class, but these tend to be used to imply "internal use".

For a stronger version of the same thing, use two leading underscores (e.g. self.__foo). Python will make a stronger attempt to prevent subclasses from accidentally overwriting the member, but determined code can of course still do so.

  • __double_leading_underscore: when naming a class attribute, invokes name mangling (inside class FooBar, __boo becomes _FooBar__boo; see below).
Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135
  • So basically, I do not need to use the underscore. Correct? – TIMEX Jul 14 '11 at 22:50
  • @TIMEX: No, you don't need to use it. And even if you do, nobody else needs to respect it. But it is a fairly common convention, so no harm is likely to come from using it either. – Daniel Pryden Jul 14 '11 at 22:52
  • 3
    @TIMEX: That's the definition of the word, "convention," yes. It is not enforced, but it does not hurt and is often helpful to follow it. – Santa Jul 14 '11 at 23:46
2

It implies internal use only (similar to private in other languages), but is not restricted like other languages.

TorelTwiddler
  • 5,996
  • 2
  • 32
  • 39
1

It just means the those attributes are for internal use only and if possible don't touch them.

Suppose You are editing some existing code and you see variables with underscore in front of them. it means that you should not edit them. Just a warning.

so

self.name = a

self._name =a

self.__name=a

are all same

Mirage
  • 30,868
  • 62
  • 166
  • 261
  • 6
    They really aren't all the same, at least not in the sense that they're interchangeable. You need to access the property with whatever name it was defined (be it 0,1 or 2 leading underscores). The underscores serve as a notice to the programmer, but the interpreter doesn't enforce the "internal use only" suggestion. – John T Jun 25 '14 at 14:04
1

It's a convention stating that clients of the class/object should avoid using those attributes if possible as they are for internal use.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358