class A(object):
def a(self, b=1):
print 'Up'
d = {1 : a}
def b( self ):
print self.d[1]
print self.b
print self.d[1].__get__( self, A )()
# print self.d[1]()
class B( object ):
def a( self ):
print 'here??'
return 10000
d = {1 : a}
def b( self ):
print 'hurray'
o = A()
o.b()
b = B()
type( o ).__dict__['b'].__get__( b, type( b ) )()
Hi Folks,
I was going through Python: Bind an Unbound Method? and http://users.rcn.com/python/download/Descriptor.htm and trying to experiment on my learning.
But, I have hit some new doubts now:-
- In the last line of my code, I'm able to use
__get__
withb
object and instance:type(b)
. This only works if methodb
is defined inclass B
. Why is it so? - Even though the last line requires me to provide a method
b
inclass B
, still the methodb
inclass A
gets called. Why is it so? - To my utter surprise, after the above step, I notice that the method
a
ofclass A
is not called by the code of methodb
ofclass A
; instead, it calls the methoda
ofclass B
. Why is it so?
I'm quite confused after seeing this behaviour. I might also need to learn more on descriptors. But, it would be a great help if you could answer my doubts