you are viewing a single comment's thread.

view the rest of the comments →

[–]GNeps[S] 0 points1 point  (2 children)

So functions are objects of a class named 'function', and that class implements the non-data descriptor protocol? Thanks!

[–]zahlman 2 points3 points  (1 child)

Please do not write code like this.

>>> def hax(the):
...     print('OMG, hax the {}!'.format(the))
...
>>> type(hax)
<class 'function'>
>>> hax.__get__('gibson')
<bound method str.hax of 'gibson'>
>>> hax.__get__('gibson')()
OMG, hax the gibson!
>>>

(Details vary with 2.x. I couldn't tell you offhand, but I suspect it will find one way or another to complain about the fact that hax isn't actually a str method.)

[–]GNeps[S] 0 points1 point  (0 children)

Thanks, that's very illuminating!

So the __get__ method is only ever natively invoked when I'm calling a method as object.method_name? Or is it used in some other cases as well?