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:
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
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.
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)
- 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)
By Leodanis Pozo Ramos • Updated May 21, 2025