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 a single leading underscore (_), signaling that they’re part of the code’s public API. Dunder names like .__init__() are a separate case. The interpreter defines them, so their leading underscores don’t mark them as non-public.
A module can also declare its public API explicitly by listing names in its __all__ attribute. Even with __all__ set, PEP 8 still calls for a single leading underscore on internal names.
Example
Here’s an example of a class with public attributes and methods:
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.
Related Resources
Tutorial
Single and Double Underscores in Python Names
Learn Python naming conventions with single and double underscores to design APIs, create safe classes, and prevent name clashes.
For additional information on related topics, take a look at the following resources:
- Python Classes: The Power of Object-Oriented Programming (Tutorial)
- Variables in Python: Usage and Best Practices (Tutorial)
- Single and Double Underscore Naming Conventions in Python (Course)
- Python's __all__: Packages, Modules, and Wildcard Imports (Tutorial)
- Python's property(): Add Managed Attributes to Your Classes (Tutorial)
- Single and Double Underscores in Python Names (Quiz)
- Class Concepts: Object-Oriented Programming in Python (Course)
- Inheritance and Internals: Object-Oriented Programming in Python (Course)
- Python Classes - The Power of Object-Oriented Programming (Quiz)
- Variables in Python (Course)
- Variables in Python: Usage and Best Practices (Quiz)
- Managing Imports With Python's __all__ (Course)
- Python's __all__: Packages, Modules, and Wildcard Imports (Quiz)
- Managing Attributes With Python's property() (Course)
- Python's property(): Add Managed Attributes to Your Classes (Quiz)
Have a question about this? Mentor AI can show you examples, compare related terms, and point you to tutorials.
By Leodanis Pozo Ramos • Updated Sept. 12, 2026