Home > Software design >  Im having "TypeError: 'float' object is not callable" error. Plz anybody who can
Im having "TypeError: 'float' object is not callable" error. Plz anybody who can

Time:10-06

class Employee:
    def __init__(self, name, salary, increment):
        self.name = name
        self.salary = salary
        self.increment = increment

    @property
    def salaryafterIncre(self):
        return self.salary * (1 self.increment)
    
    @salaryafterIncre.setter
    def salaryafterIncre(self):
        self.salary = self.salary * (1 self.increment)

e1 = Employee("shubh", 10000, 0.1)
print(e1.salaryafterIncre)
e1.salaryafterIncre()
print(e1.salary)

CodePudding user response:

you don't need to use brackets when using property. this is the feature of using property anyway

Try this;

e1 = Employee("shubh", 10000, 0.1)
print(e1.salaryafterIncre)

e1.salaryafterIncre

print(e1.salary)

CodePudding user response:

When you access the property you don't treat it as callable in python.

In your case e1.salaryafterIncre() is causing the problem. Change it to e1.salaryafterIncre

CodePudding user response:

(I'm keeping this answer basic -- this may be an oversimplification but is good enough for what you're doing).

By marking salaryafterIncre with @property, you've turned it into something that behaves a lot like a variable, rather than a method.

So when you write e1.salaryafterIncre, this actually already returns the value of this salary, which is a variable of type float. By adding () to this, you're attempting the call this floating point number as a function, which doesn't make sense! So just leave out the () when interacting with this property.

  • Related