The following example, when executed, returns:

Traceback (most recent call last):
File "/home/des/programming/python/OOP/test.py", line 13, in ?
TypeError: 'int' object is not callable

The example:

class demonstration:

    def __init__(self,number):
        self.number = number

    def number(self):

        fiveup = self.number + 5

        print fiveup


Now, I know how to fix the problem, don't let variables and methods share names.

But I want to know why it does happen. I guess because the method (number) is in fact demonstrtaion.self.number() or something like it. Because the names conflict. But I'm too much of a newb to actually know. Any answers?

10 Years
Discussion Span
Last Post by desm

Here the method is demonstration.number (it's an attribute of the class, which you hide in the instances with self.number = number .


You are right as to why this is happening; a variable and a function sharing the same name.

From within the class scope, self.number is the variable and self.number() is the function. When you initialize the class, it goes through and recognizes each function then calls __init__ (at least that's how I understand it). Since init is assigning something to self.number (which is already defined as a function), the reference to this method is over-written by the value of the passed-in parameter. Why not use self._number = number ?


Ok, thanks guys. That makes sense to me.
It's an easily fixable problem once you know it exists, but I spent ten minutes trying to figure out why I was getting a type error before hand.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.