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

Discussion in 'Python' started by pepper@rumbalski.com, Aug 21, 2007.

  1. Guest

    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
    >>>
    , Aug 21, 2007
    #1
    1. Advertising

  2. On Aug 21, 11:07 am, 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

    []
    Jonathan Gardner, Aug 21, 2007
    #2
    1. Advertising

  3. Duncan Smith Guest

    Re: confused about why i get a type error when i call an object'smethod

    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
    >
    >


    Because f.foo is a list and you're trying to call it. You need
    different names for the list and the method (both self.foo above).

    Duncan
    Duncan Smith, Aug 21, 2007
    #3
  4. Guest

    Ha! I knew it was obvious. Thank you.
    , Aug 21, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Howard
    Replies:
    2
    Views:
    366
    Michiel Salters
    Jul 4, 2003
  2. Sullivan WxPyQtKinter
    Replies:
    5
    Views:
    422
    Rene Pijlman
    Mar 7, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    863
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,765
    Smokey Grindel
    Dec 2, 2006
  5. Elf M. Sternberg
    Replies:
    15
    Views:
    260
    Matthias Reitinger
    Jul 29, 2009
Loading...

Share This Page