Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using @property versus getters and setters

Here is a 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 us to do it either way. If you would design a Python program, which approach would you use and why?

like image 350
Zaur Nasibov Avatar asked Jul 07 '11 22:07

Zaur Nasibov


People also ask

What can I use instead of getters and setters?

You may use lombok - to manually avoid getter and setter method. But it create by itself. The using of lombok significantly reduces a lot number of code. I found it pretty fine and easy to use.

What is the more pythonic way to use getters and setters?

Getters and Setters in python are often used when: We use getters & setters to add validation logic around getting and setting a value. To avoid direct access of a class field i.e. private variables cannot be accessed directly or modified by external user.

Should you always use getters and setters?

Using getters and setters, is always, in my opinion good practice. One thing you should avoid is to have external entities mess with the internal structure of your class at will. Typical example, consider having a dateOfBirth parameter.

Are getters and setters necessary C#?

Getters and setters are methods used to declare or obtain the values of variables, usually private ones. They are important because it allows for a central location that is able to handle data prior to declaring it or returning it to the developer.


2 Answers

Prefer properties. It's what they're there for.

The reason is that all attributes are public in Python. Starting names with an underscore or two is just a warning that the given attribute is an implementation detail that may not stay the same in future versions of the code. It doesn't prevent you from actually getting or setting that attribute. Therefore, standard attribute access is the normal, Pythonic way of, well, accessing attributes.

The advantage of properties is that they are syntactically identical to attribute access, so you can change from one to another without any changes to client code. You could even have one version of a class that uses properties (say, for code-by-contract or debugging) and one that doesn't for production, without changing the code that uses it. At the same time, you don't have to write getters and setters for everything just in case you might need to better control access later.

like image 113
kindall Avatar answered Oct 10 '22 00:10

kindall


In Python you don't use getters or setters or properties just for the fun of it. You first just use attributes and then later, only if needed, eventually migrate to a property without having to change the code using your classes.

There is indeed a lot of code with extension .py that uses getters and setters and inheritance and pointless classes everywhere where e.g. a simple tuple would do, but it's code from people writing in C++ or Java using Python.

That's not Python code.

like image 44
6502 Avatar answered Oct 10 '22 00:10

6502