[Tutor] TypeError: 'int' object is not callable

René Bastian rbastian at musiques-rb.org
Wed May 16 22:44:26 CEST 2012


Le Wed, 16 May 2012 14:37:53 -0600,
Modulok <modulok at gmail.com> a écrit :

> On 5/16/12, Greg Christian <glchristian at comcast.net> wrote:
> > Can anyone tell me what I am doing wrong here. When trying to call
> > the factors function from main with x = factors(Tn), getting the
> > error message: “TypeError: 'int' object is not callable”? Any help
> > would be appreciated. Thanks.
> >
> >
> > def factors(n):
> >     L = []
> >     for i in range(1, int(n ** 0.5) + 1):
> >         if (n % i == 0):
> >             L.append(i)
> >     return L
> >
> > def main():
> >     factors = 0
> >     counter = 0
> >     L = []
> >     while len(L) < 50:
> >         counter += 1
> >         L.append(counter)
> >         Tn = sum(L)
> >         x = factors(Tn)
> >         #print x
> >     print(sum(L))
> >
> >
> > if __name__ == '__main__':
> >     main()
> 
> You declared 'factors' as a variable on line 1 in main::
> 
>     factors = 0
> 
> This masks the call to the function 'factors'. You get the error
> because you assigned factors an integer and you cannot 'call' an
> integer. The easiest solution is to use another name for the variable
> 'factors' instead.
> 
> -Modulok-

If you use 'pylint', a syntax checker, you get this:

C:  1,0: Missing docstring
C:  1,0:factors: Missing docstring
W:  9,4:main: Redefining name 'factors' from outer scope (line 1)
C:  8,0:main: Missing docstring
E: 16,12:main: factors is not callable
W: 16,8:main: Unused variable 'x'


> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 



-- 
René Bastian
www.pythoneon.org


More information about the Tutor mailing list