This is my code:
class bla:
def function1():
print 1
def function2():
bla.function1()
x = bla()
x.function2()
I don’t understand why I get the error “TypeError: function2() takes no arguments (1 given)” as I don’t seem to be passing any argument to function2.
Regular methods are called with an implicit
selfreference to their object – otherwise they wouldn’t be able to access any data members ofx.They should always be declared like so:
if you want them to operate on the object (
selfis loosely equivalent to thethispointer in C++, for example).Alternatively, if you don’t care about the object (so you’re really just using the class to group some functions together), you can make them
staticlike so:In fact, that’s the only way you can call
bla.function1()without an instance ofblafrom yourfunction2.