On Sat, 01 Mar 2014 12:31:39 +0200, Marko Rauhamaa wrote:

> I need *identifiers*. I could simply define:
> 
>    class ABC:
>        A = object()
>        B = object()
>        C = object()
> 
> The program would work perfectly.
> 
> Except, if it's got a bug. I know self.abc contains either A, B or C,
> but which one? Printing out self.abc won't give me any help. I could
> print out ABC.A, ABC.B and ABC.C and see which one matches, but that's
> cumbersome.

All very good so far.

 
> The next variant is to use objects that have names:
> 
>    class Symbol:
>        def __init__(self, name):
>            self.name = name
>        def __str__(self):
>            return self.name
> 
>    class ABC:
>        A = Symbol("A")
>        B = Symbol("B")
>        C = Symbol("C")
> 
> 
> The same program still works (and still uses "is" for identity tests,
> mind you).

But why are you using identity tests? Equality will work perfectly well, 
and won't expose the implementation detail that these objects may be 
singletons. That's especially important at the next step, when you 
replace the Symbol class with regular strings. If the caller happens to 
use "C" rather than ABC.C, why is this a problem?



-- 
Steven D'Aprano
http://import-that.dreamwidth.org/
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to