why bound methods can take instance of any class as the 'self' parameter in python?

Viewed 129

the code below runs a bound method of a user defined class. i don't understand why you can send instance of any class in that bound method. according to my understanding, you should only be allowed to send instances of the 'current class' or that of 'derived classes'. please explain this. any help is highly appreciated.

class class_name:
    def function_name(self):
        print("hello, world.")

a = 10
b = [1, "mayank"]
c = (2, "mahajan")
d = class_name()

class_name.function_name(a)
class_name.function_name(b)
class_name.function_name(c)
class_name.function_name(d)
1 Answers

class_name.function_name is not a method, it's a normal function.

>>> class_name.function_name
<function __main__.class_name.function_name(self)>

class_name().function_name is a bound method.

>>> class_name().function_name
<bound method class_name.function_name of <__main__.class_name object at 0x000001A71FA76A00>>
Related