Error using Decorator in a Setter method

Asked

Viewed 55 times

-3

I’m trying to implement a simple Python code to practice using decorators in conjunction with advisory methods (Getters and Setters), but I’m getting an error.

The code is as follows::

class Pessoa():
    def __init__(self, anos):
        self._idade = 0 
        self.idade = anos

    @idade.setter
    def idade(self, anos):
        self._idade = anos

alguem = Pessoa(18)

And the error that is occurring is as follows:

NameError: name 'idade' is not defined

I do not understand, because 'age' does exist!

I searched on the internet for examples of use of decorators in conjunction with advisory methods and found some codes, but could not figure out where I am missing.

There is a way to implement only the Setter method with decorators?

1 answer

0


When assigning an _ or __ (private and protected) you should treat the attributes using a getter before passing it through the Setter. This assignment is performed through @Property, followed by the method that returns the properly treated attribute.

Other important aspects:

  1. do not start two variables of the same name, differentiated only by the private and protected attribute (it is not in any way a good practice).
  2. When assigning a class you should not assign (), this is an initiator task that you set below. The correct way to define the class is only "class Name:"

Your code would be corrected as follows:

class Pessoa:
    def __init__(self, anos):
        self._idade = anos

    @property
    def idade(self):
        return self._idade

    @idade.setter
    def idade(self, anos):
        self._idade = anos

someone = Person(18)

  • 1

    I would change in the __init__ for self.idade = anos to use the property instead of setting the attribute value directly.

  • I understood, but there were two other questions: 1° - Is there a way to implement only the Setter method using decorators? 2° - When you write self._idade = anos, this does not cause the value of years to be attributed directly to self._idade, thus disabling the created Setter method?

  • @Edy I think the recommended is that you try to delve into the question of encapsulation... these Protect and private in Python do not follow the pattern of other languages. In the case of the last questions... 1- if the attribute is Protect or private will not be accessed without the @Property, even if you use @Setter. 2- The self_age receives the value of years without any problem, the issue is that the variable information will not be accessible if the data is not treated with the encapsulation methods.

  • @Victordantas Thanks for the advice! I will seek to deepen more in the themes related to POO in Python, and mainly on encapsulation. Another thing: I forgot to explain that when I wrote self.idade = anos instead of creating another name attribute similar to the first one, I was actually using the property used in Setter and passing the value.

Browser other questions tagged

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