I'm learning flask, and now I'm reading the flask code.
I come into a block that I can not understand completely.
def implements_to_string(cls):
cls.__unicode__ = cls.__str__
cls.__str__ = lambda x: x.__unicode__().encode('utf-8')
return cls
@implements_to_string
class Test(object):
def __init__ (self):
pass
test = Test()
print(test.__str__)
print(test.__str__())
The first print shows the lambda method as:
<bound method Test.<lambda> of <__main__.Test object at 0x7f98d70d1210>>
The second:
<__main__.Test object at 0x7fcc4394d210>
So when does the x in the lambda in func implements_to_string become the cls object?
Is it just an inner mechanism I just need to remember now?
Or is there something else behind need to know?
xparameter when calling the lambda function is not theTestclass but the instancetest. Python invokes instancemethods with the instance (self) as first argument.self