Re: Enumeration idioms: Values from different enumerations



Peter Hansen <peter@xxxxxxxxxxx> writes:
> For example, if enumerations are intended to reduce the likelihood of
> certain types of errors (where the use of typical XXXX=3 "constants"
> might be more prone to errors), then perhaps this suggests that
> passing errors silently is bad. That is, trying to compare
> enumerations that should not be compared *is* an error (raising an
> exception) *because* the whole point of enumerations is to avoid
> errors in such cases.

Except it might not be an error. For instance, if I've got a list of
enum objects taken from various types (say I've got one set of enums
for days of the week, another for months of the year, and so on, and
which I use depends on whether the user wants to select days of the
week, months of the year, etc), it makes perfect sense to want to know
if a specific enum value is in the list, and the obvious way to check
it is with "my_value in enum_list". That won't work if you raise an
exception - it takes a relatively convoluted bit of code to make this
test.

Python generally uses '==' to mean "is the same value". To do that, a
simple true/false return is enough. In raising an exception, you're
making '==' carry an extra meaning (I'm not sure *what* that is,
though). Do any python builtins behave that way? How about anything in
the python standard library?

<mike
--
Mike Meyer <mwm@xxxxxxxxx> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
.



Relevant Pages

  • Re: Enumeration idioms: Values from different enumerations
    ... >>> whole point of enumerations is to avoid errors in such cases. ... >> if a specific enum value is in the list, and the obvious way to check ... >> exception - it takes a relatively convoluted bit of code to make this ... > What's being discussed here is what happens when comparing the *values* ...
    (comp.lang.python)
  • Re: Type alias in C#
    ... Since enumerations are ValueTypes, ... why does your logic for the presence of the keyword "Structure" not apply to the presence of the keyword "Enumeration" as well? ... I just can't understand how an enum can be considered a structure when it only shares one feature of structures: ... And, using your own argument, the MSDN documentation never refers to an enumeration as a "structure". ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: To const or to enum?
    ... > out on the side of using enumerations over static constants. ... be losing any sleep over an enum. ... Each node type is assigned an integral constant ...
    (comp.lang.cpp)
  • Re: Exception when uisng Intersect method
    ... C# provides syntax for exception handling through try..catch..finally blocks. ... as long as your range objects aren't NULL/Nothing, ... You can compare Addressresults ... For workbooks, since there's a possibility multiple Excel ...
    (microsoft.public.excel.programming)
  • Re: defining arithmetic for enums
    ... I am saying that incrementing an enum type is ... "Well-Behaved Enumerations," Embedded Systems Programming, ...
    (alt.comp.lang.learn.c-cpp)