pep 336: Make None Callable

Terry Hancock hancock at anansispaceworks.com
Thu Nov 4 15:12:38 EST 2004


On Thursday 04 November 2004 04:11 am, Alex Martelli wrote:
> Terry Hancock <hancock at anansispaceworks.com> wrote:
>    ...
> > > It would be more sensible to promote the addition to builtins of a 
> > > predefined null function -- one somewhat equivalent to 0, [], and {}.
> > 
> > Maybe, but I think it's worth asking -- what use is the traceback from
> > the None object when it is called?  Does it help in QA of code?  Is
> 
> Yes: it wards against the reasonably common bug of forgetting a return
> statement, for example --
> 
> def make_adder(addend):
>     def adder(augend): return addend + augend
> 
> the programmer forgot a 'return adder' as the last line -- oops, no big
> deal, it will be caught extremely soon, thanks to None not being
> callable.

> "Null Object" is a great Design Pattern (not in the Gof4 book, but still
> a classic, from a reasonably early PLOP), and I would love it if Python
> embodied it, as it embodies so many other useful ones.  But we should
> not overload None to play the role of Null, I think.  Rather, Null
> should be another object, either built-in or in some module. 
[...]

Yeah, you're probably right.  I kind of liked the lambda idiom that was
suggested up-thread (by Roman Suzi), though it's not clear to me that
replacing "lambda:" with ":" is that important:

Setting null functions to lambda seems useful.

I just noticed though, that:

noop = lambda *a, **kw: None

is illegal (apparently lambda doesn't support argument indirection (or
whatever that "*" syntax is properly called)).  Never tried it before.

Still, most of the time when you want to follow this pattern, you
probably want a consistent calling profile, and if you do use a
function to generate these functions, you can easily:

   return lambda a,b, spam=None: None

for example, following the particular profile you need.

Maybe there should be a noop built-in, though?

Cheers,
Terry

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com




More information about the Python-list mailing list