confused about why i get a type error when i call an object's method

Jonathan Gardner jgardner.jonathangardner.net at gmail.com
Tue Aug 21 14:10:41 EDT 2007


On Aug 21, 11:07 am, pep... at rumbalski.com wrote:
> I'm confused about why i get a type error when i call an object's
> method.  Here's the example code:>>> class Foo:
>
>         def __init__(self):
>                 self.foo = []
>         def foo(self):
>                 print "in foo!"
>
> >>> f = Foo()
> >>> dir(f)
>
> ['__doc__', '__init__', '__module__', 'foo']
>
> >>> f.foo()
>
> Traceback (most recent call last):
>   File "<pyshell#32>", line 1, in <module>
>     f.foo()
> TypeError: 'list' object is not callable
>
>

You've redefined what "f.foo" means in __init__. It's no longer a
class method, but an attribute of the instance--the list. You can't
call a list.

Do this to show what I mean:

>>> f.foo
[]




More information about the Python-list mailing list