Search code examples
pythonpython-decoratorssuperabc

Python TypeError: 'float' object is not callable - Using @property decorator, abstract classes, and super()


I created a short code to learn a bit more about abstract classes and the getter decorator in python as well as super. Here is my code:

from abc import ABC, abstractmethod

class Abstract_class(ABC):
    
    @abstractmethod    
    def cal(self):
        pass        
        
class Sub_class1(Abstract_class):
    
    def __init__(self, num1, num2):
        super().__init__()
        self.num1 = num1
        self.num2 = num2
        
    @property#remove to make it work
    def cal(self):
        return ( (self.num1/100) * self.num2 ) + 50
        
        
class Sub_class2(Sub_class1):
    def __init__(self, num1, num2, num3):
        super().__init__(num1, num2)
        self.num3 = num3

        

class Sub_class3(Sub_class2):
  
    def Real_num3(self):

        valor = super().cal()
        r_num3 = (valor**2) + (self.num3/100)
        return r_num3
     

x = Sub_class3( 80, 120, 10000
print( x.Real_num3() )

The code as it was stated shows me the error: TypeError: 'float' object is not callable. I figured out that by removing the @property decorator in Sub_class1 my problem was solved.

I assuming here that it is not possible to use super along with @property decorator.

Now, I am not an expert and as I said before, I am trying to get my head around this topic.

My question is why I've got that error when using the decorator @property and super? Why this is not possible?

Thanks in advance.


Solution

  • After decorating a function with @property, you no longer call it but instead use .cal to access it (remove the parenthesis).