On 14/05/13 01:49, Jim Jewett wrote:
On Sun, May 12, 2013 at 9:33 PM, Raymond Hettinger <raymond.hettinger@gmail.com> wrote:
On May 9, 2013, at 3:29 AM, Piotr Duda <duda.piotr@gmail.com> wrote:
Animals = Enum('Animals', 'dog cat bird') which violates DRY
This is a profound misreading of DRY which is all about not repeating big chunks of algorithmic logic.
DRY, like most heuristics, is about making mistakes less likely.
Mistakes are likely with huge chunks of repeated logic, because people are inclined to fix things at only one location.
Mistakes are likely with the above because it is conceptually only one location, but syntactically two -- and doing something different in the second location is a mistake that the compiler won't catch.
"Likely"? I think not. If you (generic "you", not you personally) are such a careless coder that you are *likely* to mistype the name in a *single line* like `name = Enum("name", "items...")` then there is probably no help for you. Mistakes happen to the best of us, but they are *rare*. Besides, strictly speaking setting the Enum name different to the name being bound is not necessarily an error. We can, and frequently do, define functions with one name and then bind them to a different name, e.g. in decorators. Having to repeat the name is a wart, but it is a tiny wart, and surely not worth the amount of angst it apparently causes.
The problem with
Animals = Enum('Animals', 'dog cat bird')
is that you might accidentally type
Animals = Enum('Animal', 'dog cat bird') or Anmals = Enum('Animals', 'dog cat bird')
instead.
In which case, either something will break, and you will fix the broken code, and then it will work, or nothing will break, in which case it almost certainly doesn't matter and you can pretend you did it on purpose. -- Steven