Here is a typical situation which produces the problem you describe.
class Foo(object):
def bar(self,x):
print(x)
foo=Foo()
Calling gettatrr(Foo,'bar')
returns the unbound method, Foo.bar
.
getattr(Foo,'bar')(1)
results in
TypeError: unbound method bar() must be called with Foo instance as first argument (got int instance instead)
The method, Foo.bar
, is called "unbound" because no instance (such as foo
) is going to be supplied as the first argument when called. After all, how could it when only the class Foo
was supplied?
On the other hand, if you supply an instance of the class:
getattr(foo,'bar')(1)
yields
1
since foo.bar
is a "bound" method -- foo
will be supplied as the first argument when foo.bar
is called.
PS. Your error message says, "...called with foo instance ...". Compared with the error message I posted above, it appears your class is called lowercased foo
. Note that the PEP8 style guide recommends always naming classes with a capital letter, and instances with a lowercase one. Doing so will help you avoid this error.