I am working with a library which has the following code for a Python class (edited by myself to find a minimal working example):
class Foo(object):
def __init__(self):
self._bar = 0
@property
def Bar(self):
return self._bar
If I then run the following:
foo = Foo()
x = foo.Bar()
I get an error message:
TypeError: 'int' object is not callable
So, it seems the error is telling me that it thinks Bar() is an int, rather than a function. Why?
foo.Bar is the correct way to use
property converts the class method into a read only attribute.
class Foo(object):
def __init__(self):
self._bar = 0
@property
def Bar(self):
return self._bar
@Bar.setter
def Bar(self, value):
self._bar = value
foo = Foo()
print(foo.Bar) # called Bar getter
foo.Bar = 10 # called Bar setter
print(foo.Bar) # called Bar getter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With