On Sat, 29 May 2010 06:29:43 pm spir ☣ wrote: > I'll try to clarify the purpose and use of sentinels with an example. > Please, advanced programmers correct me. A point is that, in > languages like python, sentinels are under-used, because everybody > tends to une None instead, or as all-purpose sentinel.
But None is a perfectly good sentinel, and it is very common! So how can you say that sentinels are underused? Strictly speaking, a sentinel is a guard value in an array, string, list or other sequence. See for example: http://en.wikipedia.org/wiki/Sentinel_value In this sense, sentinels are rare in Python because sequences usually know their own length and so you don't need a special sentinel to mark the end of the sequence. But I extend the term sentinel to mean any special value passed as an argument as a signal or mark. In this sense, sentinels are very common, and the most common sentinel is None. Other useful sentinels are the empty string, 0 and -1, and of course you can create your own unique sentinels using object(). > Imagine you're designing a kind of database of books; with a user > interface to enter new data. What happens when an author is unknown? The obvious way is to use the empty string for unknown or no author. This avoids needing any special checks, since the empty string is a perfectly good string, and if you need special processing, you can do so very simply: def print_book(record): if record.title: print "Title: %s" % record.title if record.author: print "Author: %s" % record.author If you want to distinguish between unknown and no author, the obvious solution is to pass "?" as unknown and "" for no author. -- Steven D'Aprano _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor