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

P

pepper

I'm confused about why i get a type error when i call an object's
method. Here's the example code: 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
 
J

Jonathan Gardner

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

['__doc__', '__init__', '__module__', '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:
[]
 
D

Duncan Smith

I'm confused about why i get a type error when i call an object's
method. Here's the example code:

def __init__(self):
self.foo = []
def foo(self):
print "in foo!"



['__doc__', '__init__', '__module__', '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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top