Peace, everyone! I\'m using Python 3.6.3 and I find strange that such construction is possible:
class TestClass(object):
def __init__(self):
self
In python 3, there is no difference between a function and a function defined in a class:
def test():
print("Hey test")
class TestClass:
def test():
print("Hey test")
test() # works
TestClass.test() # also works
Both of these are normal functions.
The magic of the implicit self argument happens when you access a function through an instance of the class, like this:
obj = TestClass()
obj.test() # throws an error because the test function doesn't accept arguments
This is when the function test is turned into the (bound) method test. You can see the difference if you print them:
print(TestClass.test)
print(instance.test)
# output:
#
# >
To sum it up:
self argument.For details about how exactly this conversion from function to bound method works, see the descriptor how-to, and specifically the section about functions.