It seems that an acceptable answer to the question
What is a method?
is
A method is a function that’s a member of a class.
I disagree with this.
class Foo(object):
pass
def func():
pass
Foo.func = func
f = Foo()
print "fine so far"
try:
f.func()
except TypeError:
print "whoops! func must not be a method after all"
- Is
funca member ofFoo? - Is
funca method ofFoo?
I am well aware that this would work if func had a self argument. That’s obvious. I’m interested in if it’s a member of foo and in if it’s a method as presented.
You’re just testing it wrong:
You error of forgetting to have in the
deftheselfargument has absolutely nothing to do withf.func“not being a method”, of course. The peculiar conceit of having thedefoutside theclassinstead of inside it (perfectly legal Python of course, but, as I say, peculiar) has nothing to do with the case either: if you forget to have a first argument (conventionally namedself) in yourdefstatements for methods, you’ll get errors in calling them, of course (aTypeErroris what you get, among other cases, whenever the actual arguments specified in the call can’t match the formal arguments accepted by thedef, of course).