public name

In Python, a public name refers to any attribute, method, variable, class, object, or module that is intended to be used from outside its immediate scope.

Python doesn’t have a strict mechanism to distinguish between public and private names like Java or C++ have. Therefore, it uses the terms public and non-public names.

The language relies on a naming convention where public names are those that don’t start with an underscore (_), signaling that they’re part of the code’s public API.

Example

Here’s an example of a class with public attributes and methods:

Python animals.py
class Dog:
    def __init__(self, name, age):
        self.name = name  # Public attribute
        self.age = age  # Public attribute

    def speak(self):  # Public method
        return f"{self.name} says Woof Woof!"

# Usage
buddy = Dog("Buddy", 5)
print(buddy.name)
print(buddy.speak())

In this example, .name, .age, and .speak() are public names, meaning you can access and use them from outside the Dog class.

Tutorial

Single and Double Underscores in Python Names

In this tutorial, you'll learn a few Python naming conventions involving single and double underscores (_). You'll learn how to use this character to differentiate between public and non-public names in APIs, write safe classes for subclassing purposes, avoid name clashes, and more.

intermediate best-practices python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated May 21, 2025