10

I have seen code in which functions/constants are prefixed with underscores. My understanding is that this indicates that they are not to be used directly. Can I do this with classes ?

class _Foo(object):
    pass

class __Bar(object):
    pass
canadadry
  • 8,115
  • 12
  • 51
  • 68
  • 7
    `__` is reserved by Python. Do not ever use this. It leads to problems with name mangling in most cases. Try to avoid thinking of this as useful. Try to think of this a "reserved" and "not for general use" and "a problem waiting to happen". And then, please take it out of your question. – S.Lott Sep 30 '11 at 11:16
  • 1
    Please explain what a "private" class might be. Can you provide a use case or a situation in which you would use this? – S.Lott Sep 30 '11 at 11:17
  • There will be exported functions which the user should use. In these functions, it will instantiate these "private" classes. – canadadry Sep 30 '11 at 11:20
  • I never add an underscore to class or variable names. Sooner or later a module needs to access this variable. I like python because it does not care about private or public variables/classes. – guettli Sep 30 '11 at 11:23
  • 2
    @guettli "Sooner or later a module needs to access this variable." The nice thing about Python is that the module _still can_ access the variable even if you prefix it with an underscore -- you're just providing a hint, not enforcing anything. – agf Sep 30 '11 at 11:28
  • 1
    "There will be exported functions"? What does "exported function" mean? You seem to be using a lot of terms from other programming languages. – S.Lott Sep 30 '11 at 12:07

4 Answers4

10

Better only use one _. This indicates that a name is private within a module.

It is not imported with the catch-all from <module> import *, and it has some other features such as "preferred destruction".

From here:

If __all__ is not defined, the set of public names includes all names found in the module’s namespace which do not begin with an underscore character ('_').

From here:

Starting with version 1.5, Python guarantees that globals whose name begins with a single underscore are deleted from their module before other globals are deleted.

Double-underscore starting class members are name-mangled.

glglgl
  • 89,107
  • 13
  • 149
  • 217
6

Yes; the single underscore usage is endorsed by PEP8 for internal-use classes.

I don't believe the double underscore usage will have any real effect most of the time, since it's used to active name mangling for class attributes, and generally a class isn't an attribute of another class (granted, it can be, in which case Python will happily mangle the name for you.)

Wooble
  • 87,717
  • 12
  • 108
  • 131
2

You can use a single underscore as the first character in any variable, but it is carries the implied meaning, "Do not use outside of the class/module unless you really know what you're doing" (eg. intended protected/internal) and it will not import if you use from <module> import *.

Using a double underscore is something you should never do outside of a class as it could mess with name mangling otherwise (and by "could", I mean, "caused me a big headache this past week because I did not realize that it does").

cwallenpoole
  • 79,954
  • 26
  • 128
  • 166
2

Yes, and this is not only a convention. When you import * from this module, names starting with underscore will not be imported.

ayanami
  • 1,588
  • 13
  • 20
  • 2
    See glglgl's answer -- that is only if `__all__` is not defined. You can include underscore prefixed names in your `__all__` if you want. – agf Sep 30 '11 at 11:29