- Python Snacks
- Posts
- Why Python doesn't have private methods
Why Python doesn't have private methods
Python relies on you to be trustworthy, but there's more to it.
If you come from a language such as C++ or Java, you’re used to having private methods:
public class Car {
public void startCar() {
System.out.println("Starting the car...");
checkEngine();
}
// Private method
private void checkEngine() {
System.out.println("Engine is in good condition.");
}
public static void main(String[] args) {
Car myCar = new Car();
myCar.startCar(); // This works fine
// myCar.checkEngine(); // This will cause a compile-time error because checkEngine() is private
}
}
However, Python doesn’t necessarily have private methods - everything is exposed.
The philosophy behind this is because we are all consenting adults here.
This simply means that Python trusts developers to act responsibly rather than enforcing strict access controls in other languages.
Specifically, the reason why there’s no true private methods:
Simplicity over complexity: Python values readability and simplicity. Strict private access modifiers add complexity without significant benefits for most Python use cases.
Flexibility for developers: Python assumes you know what you’re doing. If you really need to access an internal method, the language won’t stop you. As a result, this makes debugging, testing, and executing code more flexible.
Trust, not barriers: Python’s community culture is built on trust and clean code practices. If you’re working on someone’s code, they assume you respect their design choices.
In addition, having private methods go against the zen of Python: explicit is better than implicit, simple is better than complex, readability counts, and there should be one - and preferably only one - obvious way to do it.
If you need a private-like method, you’ll follow the naming convention of a single underscore:
def _single_underscore():
print("You probably shouldn't access this")
A single underscore tells developers that the method is meant to be used internally.
The bottom line: Python doesn’t have true private methods because it prefers to guide its developers with conventions rather than force rules. It’s not about hiding data, but about trusting developers to make the right choices.
📧 Join the Python Snacks Newsletter! 🐍
Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.
This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.
Reply