>>> class A(object): pass
>>> def func(cls): pass
>>> A.func = func
>>> A.func
<unbound method A.func>
How does this assignment create a method? It seems unintuitive that assignment does the following for classes:
- Turn functions into unbound instance methods
- Turn functions wrapped in
classmethod()
into class methods (actually, this is pretty intuitive) - Turn functions wrapped in
staticmethod()
into functions
It seems that for the first, there should be an instancemethod()
, and for the last one, there shouldn't be a wrapper function at all. I understand that these are for uses within a class
block, but why should they apply outside of it?
But more importantly, how exactly does assignment of the function into a class work? What magic happens that resolves those 3 things?
Even more confusing with this:
>>> A.func
<unbound method A.func>
>>> A.__dict__['func']
<function func at 0x...>
But I think this is something to do with descriptors, when retrieving attributes. I don't think it has much to do with the setting of attributes here.