Doubt on the Setter

Asked

Viewed 78 times

0

class Bola:

    def __init__(self, cor, circunferencia, material):
        self.__cor = cor
        self.__circunferencia = circunferencia
        self.__material = material

    @cor.setter
    def cor(self, cor):
        self.__cor = cor

    @property 
    def mostraCor(self):
        return self.__cor  

Guys, I’m doing this exercise on Jupyter Notebook, but it’s making the following mistake:

      NameError                                 Traceback (most recent call last)
  <ipython-input-12-c66d63af7d4c> in <module>
  ----> 1 class Bola:
        2 
        3     def __init__(self, cor, circunferencia, material):
        4         self.__cor = cor
        5         self.__circunferencia = circunferencia

  <ipython-input-12-c66d63af7d4c> in Bola()
        6         self.__material = material
        7 
  ----> 8     @cor.setter
        9     def cor(self, cor):
       10         self.__cor = cor

  NameError: name 'cor' is not defined

Could someone help me? The problem is in Setter, but I don’t know how to fix it.

  • 2

    Actually the problem is getter, you created a property called mostraCor, when it should just be cor: https://ideone.com/LG7cBs - in the question suggested above as duplicate, there are other recommendations also on creating getters and setters

1 answer

0


When you use the idea of Property you can start from two different approaches:

1. Use class Property as follows:

algum_atributo = property(fget=None, fset=None, fdel=None, doc=None)

Where Property will return an object that will handle the get, set, and del that have been passed as arguments to the various access forms of your "some attribute", ie, every time you want, for example make a get from the attribute, the object will call the function you passed as the fget and so on.

2. Using the @Property decorator, which works with the same function above, the difference is that under the function that will perform the get in your attribute you will decorate it with the @Property, for example:

@property
def algum_atributo(self):
    return self._algumatributo

That is, every time you perform the following command:

SeuObjeto.algum_atributo

You will call the method decorated by @Property. The important point that needs to be remembered is that, to define the Setter it must be done as follows:

@nome_funcao_decorada.setter
def nome_funcao_decorada(self,value):
    self._algumatributo = value

Where in that case, for what we did, it would be translated like this:

@algum_atributo.setter
def algum_atributo(self,value):
    self._algumatributo = value

In short, you need to use the same name as the function decorated by @Property in the Tter decorator, and even more, to facilitate use and understanding, it is recommended that the same names be used for the decorated functions.

I hope I’ve helped.

  • I understand, but why is it necessary to use the same name as the function decorated by @Property? I did using your first hint too, in it can put different function names. Of these two modes which is the best to use?

  • because notice, when you decorate a function with "@Property" you are somehow creating a Property object with that function, as you can have other functions decorated with "@Property" they use their decorated function as the main reference, that is, when you decorate a function for the "set" with the "@cor.Setter" the decorator is calling the function color that is decorated by a certain "@Property" and from it using the Setter that now already receives the function that he has just decorated, which in this case, for convenience,we also use the same name.

  • About the modes, if you have referring to the name of the function itself, it is best to always use the same name, as if it were an attribute that you want to access. Take a look here at the purpose of using the Property: https://www.programiz.com/python-programming/property

Browser other questions tagged

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