Should private attributes or public attributes be the default in Python classes?

In python we use a leading _ to make object attributes implicitly “private”. If we want to give people set/get access we can use the @property decorator. Or, if setting/getting is allowed and trivial, we can just make the attribute “public” by omitting the leading _.

My colleague and I were debating about two competing philosophies:

A: “my approach in general is to have all attributes private by default. They are escalated to public when we explicitly want the API user to get/set it”

B: “another approach could be to put make attributes private when we explicitly don’t want people to use them, but default to public with the understanding that people know they shouldn’t mess with them”

I don’t have one single clear question to ask to this community. I’d just like to get thoughts. I’d accept an answer that gives more clarity into how to think of the problem.

2

Being public requires careful design and maintenance. Don’t accept that responsibility casually.

By maintenance I mostly mean not changing what you make public in any way that would break client code. Preserving that while allowing the ability to flex other things is what I mean by careful design.

My thanks to
Basilevs for asking me to flesh that out.

However, sometimes you just need to dump some data across a boundary (which is why you can’t just move all methods to where the data is in OOP fashion). In those cases sometimes we use what is (sadly) called a Data Transfer Object (DTO). This data structure makes all it’s fields public. For some, the way to meet the ‘careful design’ requirement here is to make sure the DTO has no behavioral methods. You use it by passing it into things. Mostly something else’s constructor. When used that way it qualifies as a Parameter Object.

When I say no behavioral methods I mean if it has methods at all they’re pure getters and setters that only move data. This keeps it’s focus on being a data structure that other (real) objects use.

With that in mind, when not creating a DTO, when building a behavior object, then sure, default to private. Because being public is work.

12

I’d say neither.

In Python, you don’t want to make things public that should be private (like in C++) but you also don’t want to make things private that should be public. Because if you don’t make some user-relevant property public, the users of your class will just access the private attribute. And then your “private” attribute will become part of the de-facto public API. Your users will just teach each other “if you need to know the filkiness of an Esnesnon instance, you’ll need to do esnesnon._filkiness. Yeah it’s not documented, but it works.”

Of course there are very good reasons why you don’t want to expose every attribute of every class, which is why I would advocate evaluating the need for underscore prefixing on a case-by-case basis.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật