[Baypiggies] What's the "Pythonic" way to access class attributes?
ken at seehart.com
Tue Oct 11 13:49:59 EDT 2016
The hazards associated with accessing attributes that are intended as private or protected depend entirely on the situation at hand. In many cases the main issue is that the module defining the class is subject to change and there is no contract to maintain compatibility with respect to those attributes. As with all rules, avoid blind dogma, try to understand the actual consequences, and remember we are grown ups.
We are all. Free. To do. Whatever. We want. To do. - Richard Bach
Regarding getters and setters, I'm convinced that these are utterly useless in Python, and often a horrendous waste of time. They are often quite necessary for future-proofing in statically typed languages such as Java, but there exists no scenario in Python in which that pattern can yield an increase in robustness, resilience, or productivity.
I rarely get this passionate and one sided about an issue, but I recently witnessed a LOC increase of around 8000 lines attributable to this horrific anti-pattern, and along with it a substantial decrease in the resilience of the code base. It was horrible. I and my colleagues were unable to compose any hypothetical future scenario for which this provided any protection. In fact, ironically, there were a couple bugs caused by failure to keep the setters up to date! More code = more bugs.
I find that it is usually best to just "keep it simple, stupid". If an attribute makes sense in terms of the View, and currently represented in the Model as an ordinary attribute, just make it public and don't make a big fuss over OOP ideas about hiding implementation details, (unless you can think of a "clear and present" reason for doing so). The reason you can get by with this in Python or simple, but subtle: an ordinary attribute is already syntactically equivalent to a property, so you've effectively got implementation hiding for free. Sure, you're exposing data members, but you haven't committed to doing so.
What if the data model changes (e.g. the attribute is now stored in remote database)? If you are coming from a Java background, you can have your 30 second panic attack: Bloody Hell! We forgot to publish an accessor interface! We are so humped! Then relax. This is Python. It's all good. It's never too late. Just refactor the attribute into a @property and all is well.
~ken (Sent from my phone)
On Oct 11, 2016 2:29 AM, Braun Brelin <bbrelin at gmail.com> wrote:
> Hello all,
> I just wanted to get the list's opinion on something.
> Since Python doesn't really have the concept of private and protected
> attribute access protections a la Java or C++ (Yes, I know you can use
> the __ and _ prefixes to declare something private or protected in
> Python, but that really only does name-mangling on the attribute name
> rather than explicitly disallow access to the attribute from outside the class), is it better to still access the attribute directly a la
> Foo.bar = 10 or use a setter/getter approach like Foo.setBar(10)?
> It seems to me that option one is better, especially if I can use a descriptor with the attribute.
> Any thoughts?
> Braun Brelin
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Baypiggies