2

Hey guys so I am getting this "TypeError: 'int' object is not callable" error when I run this code, I did some research and I think it's something with my variable naming? Could someone please explain to me what is wrong?

class account(object):
    def set(self, bal):
        self.balance = bal
    def balance(self):
        return balance

Here is my example run:

>>> a = account()
>>> a.set(50)
>>> a.balance()
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    a.balance()
TypeError: 'int' object is not callable
1
  • Where exactly is the error pointing to? Commented Apr 5, 2013 at 19:25

1 Answer 1

13

You are masking your method balance with a instance attribute balance. Rename one or the other. You could rename the instance attribute by pre-pending it with an underscore for example:

def set(self, bal):
    self._balance = bal

def balance(self):
    return self._balance

Attributes on the instance trump those on the class (except for data descriptors; think propertys). From the Class definitions documentation:

Variables defined in the class definition are class attributes; they are shared by instances. Instance attributes can be set in a method with self.name = value. Both class and instance attributes are accessible through the notation “self.name”, and an instance attribute hides a class attribute with the same name when accessed in this way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.