Get and Set using PROPERTY - Python3

Asked

Viewed 487 times

1

As an apprenticeship of Property, created a small currency converter. See:

class converteMoeda():


  def __init__(self, dollar=0, real=0):
    self.dollar = dollar
    self.real = real

  def toDollar(self):
    dollar = self.real/self.dollar
    print("Convertendo Reais para Dólar.")
    return(dollar)

  def toReais(self):
    reais = self.dollar/self.reais
    print("Convertendo de Dólar para Reais.")
    return(reais)

  def setDollar(self,dollar):
    print("Gravando valor do dólar.")
    self._dollar = dollar
  def setReais(self,real):
    print("Gravando valor do real.")
    self._real = real

  def getDollar(self):
    print("Recuperando valor do dólar.")
    return(_dollar)
  def getReais(self):
    print("Recuperando valor do Real.")
    reutrn(_real)

  real = property(getReais,toReais)
  dollar = property(getDollar,toDollar)

c = converteMoeda()
c.toReais(3.17)

However, for a reason that I could not identify, I am generating an error in the number of parameters:

Traceback (most recent call last):
  File "python", line 35, in <module>
  File "python", line 5, in __init__
TypeError: toDollar() takes 1 positional argument but 2 were given

Could someone help me on this adventure?

  • The method toDollar only receives self as a parameter. If it functions as a Setter property, it must receive the new property value as parameter as well.

  • By the way, your code has several syntax errors.

1 answer

2

I created a small example that by default has the value of the dollar in 3.3, Voce can change and access this value in a class instance through the property and Setter us.

(TL;DR)

class Convert:

    def __init__(self):
        self.us = 3.3

    def to_dollar(self, real=0):
        return real/self.__us

    @property
    def us(self):
        return self.__us

    @us.setter
    def us(self, us):
        self.__us = us

c = Convert()
reais = 16.67

Using the @property us:

print ('valor atual do dollar: ', c.us)
valor atual do dollar:  3.3

Using to_dollar method for conversion:

print('Conversão de',reais,'reais em dollar: ',c.to_dollar(reais))
Conversão de 16.67 reais em dollar:  5.051515151515153

Using the us.setter:

c.us = 3.10
print ('Valor Atualizado do dollar: ',c.us)
Valor Atualizado do dollar:  3.1

New conversion after update with Setter:

print('Conversão de',reais,'reais em dollar: ',c.to_dollar(reais))
Conversão de 16.67 reais em dollar:  5.37741935483871

See the code running in repl.it.

  • I really appreciate your reply! It works fine. But my biggest question is: It is possible to perform two operations using the property? In my example, I do Dollar->Real and Real->Dollar conversion. You could tell me if it’s possible?

  • @Snowbg Property does not "perform operations" only returns the value of a "property", you can perform several operations to return that property. very simple.

  • @Snowbg p/ do what you want just create a new method to_real(self, us) return us*self.__us and call it so c.to_real(valor)

Browser other questions tagged

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