Consider the following code:
class A(object):
a = []
@classmethod
def eat(cls, func):
print "called", func
cls.a.append(func)
class B(A):
@A.eat
def apple(self, x):
print x
A.eat(lambda x: x + 1)
print A.a
Output :
called <function apple at 0x1048fba28>
called <function <lambda> at 0x1048fbaa0>
[<function apple at 0x1048fba28>, <function <lambda> at 0x1048fbaa0>]
I expected A.a to be empty as we have not even created an object.How are the 2 function getting added here?What exactly is causing eat to get called 2 times?
class A: print('hello')would print "hello" when loading the class. Both@A.eatandA.eat()are executed statements when the class is loaded, even without theprint A.ayou would get the first 2 printed outputs.