Class Method With No Arguments Produces TypeError
Solution 1:
You call the methods as self.test()
. You should mentally translate that to test(self)
to find out how the call will be "received" in the function's definition. Your definition of test
however is simply def test()
, which has no place for the self
to go, so you get the error you observed.
Why is this the case? Because Python can only look up attributes when specifically given an object to look in (and looking up attributes includes method calls). So in order for the method to do anything that depends on which object it was invoked on, it needs to receive that object somehow. The mechanism for receiving it is for it to be the first argument.
It is possible to tell Python that test
doesn't actually need self
at all, using the staticmethod
decorator. In that case Python knows the method doesn't need self
, so it doesn't try to add it in as the first argument. So either of the following definitions for test
will fix your problem:
def test(self):
print('test')
OR:
@staticmethod
def test():
print('test')
Note that this is only to do with methods invoked on objects (which always looks like some_object.some_method(...)
). Normal function invocation (looking like function(...)
) has nothing "left of the dot", so there is no self
, so it won't be automatically passed.
Solution 2:
Pass self
to your test
method:
def test(self):
print('test')
You need to do this because Python explicitly passes a parameter referring to the instantiated object as the first parameter. It shouldn't be omitted, even if there are no arguments to the method (because of the error specified).
Solution 3:
Python always passes the instance as the first argument of instance methods, this means that sometimes the error messages concerning the number of arguments seems to be off by one.
class testclass:
def __init__(self,x,y):
self.x = x
self.y = y
self.test()
def test(self): ## instance method
print('test', self)
if __name__ == '__main__':
x = testclass(2,3)
If you don't need access to the class or the instance, you can use a staticmethod as shown below
class testclass:
def __init__(self,x,y):
self.x = x
self.y = y
self.test()
@staticmethod
def test():
print('test')
if __name__ == '__main__':
x = testclass(2,3)
A classmethod is similar, if you need access to the class
, but not the instance
class testclass:
def __init__(self,x,y):
self.x = x
self.y = y
self.test()
@classmethod
def test(cls):
print('test', cls)
if __name__ == '__main__':
x = testclass(2,3)
Post a Comment for "Class Method With No Arguments Produces TypeError"