[Baypiggies] What's the "Pythonic" way to access class attributes?

Sean Perry shaleh at speakeasy.net
Tue Oct 11 14:17:53 EDT 2016


get/set is a bad idea in almost any language actually. The OO design 
folk will tell you the methods should be action and use oriented. 
 
foo.unlock() instead of foo.setLocked(false)
 
foo.activateEngine()
 
etc. 
 
The biggest value for a function of some sort in my opinion is it is 
easier to refactor later. Finding all of the places that foo.bar = 1 
can be annoying and worse you fall into idioms like:
 
foo.bar = 1
if reason:
   foo.bar += 2  # yikes, I have to refactor that too. 

On Tue, 11 Oct 2016 10:29:33 +0100, Braun Brelin  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. 
 


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20161011/e8a42c95/attachment-0001.html>


More information about the Baypiggies mailing list