Param decorator - can you suggest improvements

Dan Sommers dan at tombstonezero.net
Fri Jan 18 15:18:48 CET 2013


On Fri, 18 Jan 2013 09:10:34 +0000, Steven D'Aprano wrote:

> On Fri, 18 Jan 2013 03:38:08 +0000, Dan Sommers wrote:

>> This, or something like this, is very old:
>> 
>> sentinel = object()
>> class Magic:
>>     def __init__(self, value):
>>         self.value = value
>>     def __call__(self, value=sentinel):
>>         if value != sentinel:
>>             self.value = value
>>         return self.value
> 
> There's not really any magic in that :-)

Well, not any more, no, but this one has stuck in my mind because of the 
magic it revealed the first time I saw it.  It was definitely one of 
those Aha! moments for me.

> Better to use "if value is not sentinel" rather than != because the
> caller might provide a custom object that compares equal to sentinel.
> 
> Also you should name it SENTINEL, or even _SENTINEL, to indicate that it
> is (1) a constant, and (2) a private variable.

Both fair points.

> I believe that C++ calls it a "functor", not to be confused with what
> Haskell calls a functor, which is completely different.

But we like overloaded terminology!  ;-)

Dan



More information about the Python-list mailing list