From: Steven D'Aprano on 16 Jun 2010 12:34 On Wed, 16 Jun 2010 09:17:47 -0700, Stephen Hansen wrote: > Leading-and-trailing double underscores are explicitly reserved for > Python to define as Special. That part is correct. But of course Python doesn't prevent you from ignoring this rule (more of a guideline really). > They also imply a slightly different > calling semantic: normal leading-and-trailing double underscore methods > bypass instance lookup. This is semi-correct. Operator overloading and other special method lookup bypasses instance lookup and calls type(instance).__method__ but that has nothing to do with the double-underscore methods themselves. They operate normally if you call them by hand: >>> class C(object): .... def __add__(self, other): .... return "special" .... >>> c = C() >>> c.__add__ = lambda self, other: "not very special" >>> c + 4 'special' >>> c.__add__(c, 4) 'not very special' -- Steven
From: Stephen Hansen on 16 Jun 2010 12:49 On 6/16/10 9:34 AM, Steven D'Aprano wrote: > On Wed, 16 Jun 2010 09:17:47 -0700, Stephen Hansen wrote: > >> Leading-and-trailing double underscores are explicitly reserved for >> Python to define as Special. > > > That part is correct. But of course Python doesn't prevent you from > ignoring this rule (more of a guideline really). Agreed. Its a namespace reserved for Python itself to define. But "reserved" in the more traditional Python sense of it saying, "Please keep your hands off", and not, "SyntaxError: Illegal creation of new special method". >> They also imply a slightly different >> calling semantic: normal leading-and-trailing double underscore methods >> bypass instance lookup. > > This is semi-correct. Operator overloading and other special method > lookup bypasses instance lookup and calls type(instance).__method__ but > that has nothing to do with the double-underscore methods themselves. > They operate normally if you call them by hand: Well, yes; I was simplifying slightly because people don't really call them by hand in the normal course of events (and eventually you'll pass the object into some internal where it most certainly doesn't get called that way). But true, that's more accurate. :) -- Stephen Hansen ... Also: Ixokai ... Mail: me+list/python (AT) ixokai (DOT) io ... Blog: http://meh.ixokai.io/
First
|
Prev
|
Pages: 1 2 Prev: Jewish Pirates of the Caribbean Next: Coroutines: unexpected behaviour |