0

Should I use underscore before function name in single-file simple python script? For example, in this script should I add underscore before definition of f and g? This script won't be imported from other files.

def f(x): # or _f(x)?
    return x * 2

def g(x): # or _g(x)?
    return x ** 2

def main():
    x = f(100)
    print(g(x))

if __name__ == "__main__":
    main()

I read many documents about usage of underscores in python. Many of them says underscores are about OOP-style programming and how import statement works. However, in simple one-file script, I can't find good answer.

What is better pattern?

minty99
  • 327
  • 1
  • 2
  • 9
  • Generally speaking leading underscores are meant to indicate "weak internal use". See [pep8](https://www.python.org/dev/peps/pep-0008/#id34) for more. – 0x263A Jan 05 '22 at 05:47
  • Does this answer your question? [What is the meaning of single and double underscore before an object name?](https://stackoverflow.com/questions/1301346/what-is-the-meaning-of-single-and-double-underscore-before-an-object-name) – Jan Wilamowski Jan 05 '22 at 06:00
  • @JanWilamowski I already read the the answer, but I can't judge the functions in my tiny script are _weak internal use_. How can I choose it? – minty99 Jan 05 '22 at 06:09
  • That only matters once you combine multiple modules and you want to hide certain aspects of your implementation. It's not necessary in your simple case. – Jan Wilamowski Jan 05 '22 at 06:13
  • 1
    @minty99, once your dubts are answered, you can accept the most suitable answer (or at least one that worked for you) so that others can be sure that it's a working answer. – FLAK-ZOSO Jan 05 '22 at 06:17

2 Answers2

1

_ is used to let developers know that the variables and methods are private and should not be modified externally. So use _ if you want to keep the functionality within the class.

NOTE: using _ will not restrict the use of these methods or variables though, it's just a way of representation.

  • However I don't use classes in my script. (just small script..) How can I judge the function is _internal_ ? – minty99 Jan 05 '22 at 06:07
1

The most popular programming languages (such as Java and C/C++) have this kind of syntax to declare private attributes and methods of a class instance:

class MyClass {
    private:
        int _function() {
            // Some code here
        }
    public:
        bool _foo() {
            // Some code here
        }
}

Python doesn't have (and will probably never have) a syntax like this, so they simply create a conventional name to make developers understand that a method is private and should never be accessed from outside the class.


According to PEP-8:

We don't use the term "private" here, since no attribute is really private in Python (without a generally unnecessary amount of work).

According to Python 2 and Python 3 class documentation:

“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member).


Using _ may be useful for a tecnique explained below.

According to Python 3 class documentation:

Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling.


NOTE: The Python documentation mostly talks about "internal use" referring to classes, but it can be referred to modules too, for example:

# main.py
import myModule
functionThatCanBeImported()

# myModule.py
def functionThatCanBeImported(): pass
def _functionThatShouldNeverBeImported(): pass

If somebody creates a package, they don't have to put private functions in the documentation, since they are for internal scope and explainations about them could be useful only to developers.

FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
  • However I didn't use classes in my tiny script - because I think it is overkill. How to judge a function is private in this usage? – minty99 Jan 05 '22 at 06:11
  • 1
    @minty99, nice question, I've just edited the answer, hope I've been helpful – FLAK-ZOSO Jan 05 '22 at 06:12
  • Since I will not import my standalone script from other files, all functions except `main` should be single underscored, right? How do you think about this? – minty99 Jan 05 '22 at 07:36
  • 1
    If you are writing a program that will never be imported by anyone there's nothing wrong with writing all without underscores. – FLAK-ZOSO Jan 05 '22 at 08:50