functions, list, default parameters
steve at REMOVE-THIS-cybersource.com.au
Sun Nov 7 00:07:52 CET 2010
On Sat, 06 Nov 2010 12:37:42 +0000, Mark Wooding wrote:
>> > _missing = ['missing']
>> A curious choice for the sentinel value. We're not using Python 1.5 any
> Two reasons. Firstly, this comes from my Lisp background: making a list
> is the obvious way of producing an unforgeable object. Secondly, if you
> see an objects that prints as ['missing'], you have a chance of working
> out where it came from; for a loose object() that's kind of hard without
> a wabbit hunter.
But surely you shouldn't expect to only have the repr() of the value to
go by. You'll have a traceback, which shows you not just that you have a
sentinel, but where it ended up.
It seems silly to me to fear seeing a bare object() more than all the
other objects that give no context as to where they come from. If you see
None, or , or 1, out of context, you're going to have trouble working
out what it's used for too. At least object() is rare -- it shouldn't be
hard to do a search through the source code to find where it is created
and from there find out where it is used.
To me, the idiom x = object() says "missing value" MORE strongly than the
Lisp-ism x = ['missing']. After all, x = object() has no state, and no
methods to speak of, what else could it be other than a sentinel? But
['missing'] has state, it's mutable, it has methods up the whazoo. If you
fear your code will expose it to the caller, why don't you fear that your
code will mutate it to ['found now'], or something equally confounding?
The problem with the Lisp-ism is that the object ['missing'] could have
many uses and could be modified. Without context, I'd be wondering
whether I'd stumbled across a really short list of gerunds ("the missing
kettle") or verbs. If you're writing code that does a lot of text
processing, that won't be an outlandish thought.
If I had a lot of sentinels in the one module and for some bizarre reason
couldn't re-use them, and so needed many different unique sentinels that
needed to be distinguished, then I'd probably lean towards using ['the']
['Lisp'] ['idiom']. But for just one, I stick with my claim that object()
says "missing" more strongly than ['missing'], strange as it may seem :)
More information about the Python-list