7
I am on time trying to understand how this issue of python properties works. But the problem is that in all the tutorials net out only find the damn example with only one attribute. I’m looking for an example where it has more to do with everyday life. Type with more attributes like name, age, phone, access (Boolean), etc.. I have questions like, for example, should you create a @Property, @Setter for each attribute? How would be the example below if we had the data of a person like name, phone, age, email, etc....
class Person(object):
def __init__(self):
self._name = ''
@property
def name(self):
print "Getting: %s" % self._name
return self._name
@name.setter
def name(self, value):
print "Setting: %s" % value
self._name = value.title()
@name.deleter
def name(self):
print ">Deleting: %s" % self._name
del self._name
Exactly as I imagined it. I was resistant to accept it that way. For I had watched a video of Luciano Ramalho where he "put the stick" criticizing the gets and sets of java. But it seems to me that in the end it’s the same.
– Devel Silva
@Develsilva I’m not necessarily recommending that you use properties unnecessarily. You just asked what the syntax was. I definitely don’t recommend using them just for use. They have their value but can also be misused.
– sergiopereira
what would be a really necessary case? For example in common entities, Person, Customer, Car, etc. What to use for such cases? gets and common sets, java pattern?
– Devel Silva
If you are simply setting and reading from a private field, I consider the use of property unnecessary. On the other hand, if you want to create a property read-only (or write-only), or if you want to validate the past value, or create a calculated/derived property from others, then this use makes more sense.
– sergiopereira
An example, in a class
Pessoa
use simple fields for all attributes, such asnome
anddata_de_nascimento
, and use a propertyidade
which is derived fromdata_de_nascimento
– sergiopereira