Consider the following snippet.
import types
def deff(s):
print(f"deff called, {s=}")
lam = lambda s: print(f"lam called, {s=}")
class Clbl:
def __call__(s):
print(f"__call__ called, {s=}")
clbl = Clbl()
print(type(deff) == types.FunctionType) # True
print(type(lam) == types.LambdaType) # True
print(type(clbl) == Clbl) # True
class A:
deff = deff
lam = lam
clbl = clbl
a = A()
a.deff() # deff called, s=<__main__.A object at 0x7f8c242d4490>
a.lam() # lam called, s=<__main__.A object at 0x7f8c242d4490>
a.clbl() # __call__ called, s=<__main__.Clbl object at 0x7f8c24146730>
print(a.deff is deff) # False
print(a.lam is lam) # False
print(a.clbl is clbl) # True
print(type(a.deff) == types.FunctionType) # False
print(type(a.lam) == types.LambdaType) # False
print(type(a.clbl) == Clbl) # True
print(type(a.deff) == types.MethodType) # True
print(type(a.lam) == types.MethodType) # True
Why are a.deff
and a.lam
methods but a.clbl
not a method? Why do the expression a.deff()
and a.lam()
pass a
as the first argument to the corresponding functions while a.clbl()
doesn't?