python

Is there difference between _function and function?


When I read the source code of a project:

@profiler.trace
def default_quota_update(request, **kwargs):
    novaclient(request).quota_classes.update(DEFAULT_QUOTA_NAME, **kwargs)


def _get_usage_marker(usage):
    marker = None
    if hasattr(usage, 'server_usages') and usage.server_usages:
        marker = usage.server_usages[-1].get('instance_id')
    return marker


def _get_usage_list_marker(usage_list):
    marker = None
    if usage_list:
        marker = _get_usage_marker(usage_list[-1])
    return marker

You can see there are _function and function, is there something different between them?


Solution

  • It's a way programmers use to communicate that these functions (and attributes as well) should be kept "private".

    Quoting PEP-8:

    _single_leading_underscore: weak "internal use" indicator.

    e.g. 'from M import *' does not import objects whose name starts with an underscore.