Understanding the pythonic way of dealing with properties

Asked

Viewed 492 times

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

1 answer

5


When you declare the getter, the method can then be used to mark the setter and the deleter (I omitted the deleter of my example to get shorter).

class Pessoa(object):

    def __init__(self):
        self._nome = None
        self._idade = None
        self._telefone = None

    @property
    def nome(self):
        print "Getting: %s" % self._nome
        return self._nome

    @nome.setter
    def nome(self, value):
        print "Setting: %s" % value
        self._nome = value

    @property
    def idade(self):
        print "Getting: %s" % self._idade
        return self._idade

    @idade.setter
    def idade(self, value):
        print "Setting: %s" % value
        self._idade = value

    @property
    def telefone(self):
        print "Getting: %s" % self._telefone
        return self._telefone

    @telefone.setter
    def telefone(self, value):
        print "Setting: %s" % value
        self._telefone = value
  • 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.

  • @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.

  • 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?

  • 1

    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.

  • An example, in a class Pessoa use simple fields for all attributes, such as nome and data_de_nascimento, and use a property idade which is derived from data_de_nascimento

Browser other questions tagged

You are not signed in. Login or sign up in order to post.