Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python property getter and setter decorator not callable

Tags:

python

I'm doing something similar to the following

class Parrot(object):
    def __init__(self):
        self.__voltage = 100000

    @property
    def voltage(self):
        """Get the current voltage."""
        return self.__voltage

However it sees the voltage property as an int so when I call like so

p = Parrot()
print(p.voltage())

I get

TypeError: 'int' object is not callable

I've tried with one and two underscored to mangle the voltage property name.

like image 397
magwe25 Avatar asked May 21 '26 23:05

magwe25


1 Answers

The whole point of the getter is that it returns the value without being called. p.voltage returns the integer object, so running p.voltage() is equivalent to 100() or something.

There can still be cases where you want to call the value of a property, like if the value is itself a function. But you don't need it here.

like image 69
Peter DeGlopper Avatar answered May 23 '26 12:05

Peter DeGlopper