Auto getter, setter and deleter installation in python

Well, I read about using the @property decorator in Python, and not about special methods of getting, setting, and deleting. The following is an example code example.

class Person(object): def __init__(self): self._firstName = None self._lastName = None self._age = None @property def firstName(self): return self._firstName @firstName.setter def firstName(self, val): self._firstName = val @firstName.deleter def firstName(self): del self._firstName @property def lastName(self): return self._lastName @lastName.setter def lastName(self, val): self._lastName = val @lastName.deleter def lastName(self): del self._lastName @property def age(self): return self._age @age.setter def age(self, val): self._age = val @age.deleter def age(self): del self._age Peter = Person() Peter.firstName = 'Peter' Peter.lastName = 'Smith' Peter.age = 21 print ' '.join([Peter.firstName, Peter.lastName]) 

As you can see, most of the code in the example is devoted to defining methods.

My question is, is there a way to automatically assign a getter, setter and deleter to each of my variables? In my actual code, I have a lot more variables, and it seems that many duplicate codes are set by each method.

I understand that for simple cases this may not be necessary, but I would prefer to code the methods now if I ever decided to increase the complexity of the class.

+6
source share
3 answers

Keep it simple.

If you have such functions, just use a public variable. I.e:

 class Person(object): def __init__(self): self.firstName = None self.lastName = None self.age = None 

excellent. The best approach to use is to require data as part of the constructor, thus:

 class Person(object): def __init__(self, firstName, lastName, age): self.firstName = firstName self.lastName = lastName self.age = None person = Person('Peter', 'Smith', 21) 

If you are worried about what name is, you can always be specific:

 person = Person(firstName='Peter', lastName='Smith', age=21) 

In the future, if you need to make it more complex, you can add getters / setters where necessary.

Another consideration is that given the constructor, you can create a string conversion function in the Person object:

 def __str__(self): return ' '.join([self.firstName, self.lastName]) 

This allows you to use it as:

 person = Person(firstName='Peter', lastName='Smith', age=21) print(person) 

therefore, you do not need to know how it is implemented internally.

Another consideration is that Chinese / Japanese names first put a surname, people can have several surnames (especially in Spain) and can have middle names.

+3
source

The whole point of the property decorator is that you do not need to define it from the very beginning. Accessing an attribute directly is the same syntax as accessing it through a property. Therefore, you should completely abandon it until you actually have complex logic, and only then go to the properties.

+2
source

Manually defining a setter and deleter is an obvious way of saying: "I spent a lot of time creating this interface, and you can freely install, delete and receive this property without any problems with code violation."

Otherwise, you can simply use instance variables without a property decorator.

 def __init__(self): self.firstName = None self.lastName = None self.age = None 

On a side note, you'll probably want to take a look at PEP-8 for legend and pythonic style notation.

0
source

All Articles