Hey Denis!
On Saturday May 29 2010 10:29:43 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. > > 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? A proper way, I > guess, to cope with this case, is to define a sentinel object, eg: > UNKNOWN_AUTHOR = Object() > There are many ways to define a sentinel; one could have defined "=0" or > "=False" or whatever. But this choice is simple, clear, and secure because > a custom object in python will only compare equal to itself -- by default. > Sentinels are commonly written upercase because they are constant, > predefined, elements. I waited for a thread like this to appear, because I have a quirky, but IMHO elegant, solution for those kinds of variables: class EnumMeta(type): def __repr__(self): return self.__name__ class Enum(object): __metaclass__ = EnumMeta Objects are created by inheriting from the Enum class. (Not by instantiating it.) >>> class EAST(Enum): pass >>> class WEST(Enum): pass >>> class NORTH(Enum): pass >>> class SOUTH(Enum): pass The objects know their name, and when printed their name is printed. In this respect they behave similarly to None. >>> print NORTH, SOUTH, EAST, WEST NORTH SOUTH EAST WEST I call the class Enum, but this is certainly the wrong term since the values are not enumerated. But is Sentinel the right term for something like this? I thought a sentinel is a soldier who guards something. Hello English native speakers! What is a good name? Eike. P.S. By the way Denis, an earlier thread from you on the subject got me thinking about it. _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor