properties - Python @property versus getters and setters -


here pure python-specific design question:

class myclass(object):     ...     def get_my_attr(self):         ...      def set_my_attr(self, value):         ... 

and

class myclass(object):     ...             @property     def my_attr(self):         ...      @my_attr.setter     def my_attr(self, value):         ... 

python lets either way. if design python program, approach use , why?

prefer properties. it's they're there for.

the reason attributes public in python. starting names underscore or 2 warning given attribute implementation detail may not stay same in future versions of code. doesn't prevent getting or setting attribute. therefore, standard attribute access normal, pythonic way of, well, accessing attributes.

the advantage of properties syntactically identical attribute access, can change 1 without changes client code. have 1 version of class uses properties (say, code-by-contract or debugging) , 1 doesn't production, without changing code uses it. @ same time, don't have write getters , setters in case might need better control access later.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

jdbc - Not able to establish database connection in eclipse -