5

I created two modules, one with

def _func():
    print "hi"

and another

def func():
    print "hi"

When I use the help function on the module including the first function, the help module doesn't display this function. As opposed the second example where this function shows in the help output. Is there any functional difference aside from the use of the help function?

Bentley4
  • 10,678
  • 25
  • 83
  • 134
  • 1
    possible duplicate of [The meaning of a single- and a double-underscore before an object name in Python](http://stackoverflow.com/questions/1301346/the-meaning-of-a-single-and-a-double-underscore-before-an-object-name-in-python) – Wooble Apr 06 '12 at 14:33
  • Please write code that can be executed: add `def` to define functions. – rubik Apr 06 '12 at 14:39
  • Not a duplicate. The other answer can even be a superset of the answer here. – jsbueno Apr 06 '12 at 14:42

4 Answers4

13

Yes, there is an (admittedly subtle) difference functionality-wise. Let's assume you have a module A.py:

foo = 1
_bar = 2

Observe:

>>> from A import *
>>> foo
1
>>> _bar
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '_bar' is not defined

The default behaviour if you do an import * is that members with a leading underscore are not imported. You can override that behaviour by specifying an __all__ list:

__all__ = ['foo', '_bar']

foo = 1
_bar = 2

Now:

>>> from A import *
>>> (foo, _bar)
(1, 2)

By the way, __all__ also overrides the list of members shown by help() or pydoc:

$ pydoc A | cat
 ...
DATA
    __all__ = ['foo', '_bar']
    _bar = 2
    foo = 1
 ...
Niklas B.
  • 92,950
  • 18
  • 194
  • 224
3

It's a python convention that any names starting with an underscore are not considered as part of a public api, thus users of a module or class that contains functions beginning with underscores won't be told about the existence of these functions by normal access. Of course, since it's just a convention, it can be bypassed, but as a general rule of thumb you can consider anything starting with an underscore to be hidden from code outside the module/class.

Here is some more information from the python documentation: http://docs.python.org/tutorial/classes.html#tut-private

eltommo
  • 346
  • 3
  • 15
3

There is a difference for module level names staring with "_"s : they are not exported when you do use from module import * -

This behavior can be overriden by letting the module have a list named __all__ which lists all the attributes you want to export from the module.

Though:

import module
module._func

will work with no differences. As the other answers imply: the "_" is an indicator that the name shouild be reserved for private use of the module/class/API - however, in the sole case of "from module import *", and and authomatic help - as you se in your case, the interpreter does treat then differently.

jsbueno
  • 99,910
  • 10
  • 151
  • 209
2

Underscores are a convention usually used to indicate a private member of a module or class.

blokeley
  • 6,726
  • 9
  • 53
  • 75