[Python-Dev] PEP 435 - ref impl disc 2
Ethan Furman
ethan at stoneleaf.us
Tue May 14 04:43:37 CEST 2013
On 05/13/2013 07:36 PM, Ethan Furman wrote:
> Here's your code, revamped. I did make a slight change in the meta -- I moved the name assignment above the __init__
> call so it's available in __init__.
>
> --8<--------------------------------------------------------
> from ref435 import Enum
> from flags import IntET
>
> class NIE1( IntET, Enum ):
> x = 1
> y = 2
> z = 4
> def __new__(cls, value):
> member = IntET.__new__(cls, 'temp', value)
> member._value = value
> return member
> def __init__(self, value):
> self._etname = self._name
>
> print( repr( NIE1.x.value ))
> print( repr( NIE1.x + NIE1.y ))
> print( repr( NIE1.x + ~ NIE1.y))
> print( repr( NIE1.x + ~ 2 ))
> print( repr( NIE1.z * 3 ))
>
>
> print( repr( NIE1( 1 ) + NIE1(2)))
> print( repr( NIE1( IntET('NIE1.x', 1 )) + NIE1(2)))
> --8<--------------------------------------------------------
>
> and my results:
>
> 1
> IntET('(x + y)', 3)
> IntET('(x + ~y)', -2)
> IntET('(x + -3)', -2)
> IntET('(z * 3)', 12)
> IntET('(x + y)', 3)
> IntET('(x + y)', 3)
Forget to mention the good part -- in the custom __new__ you are able to set the value to whatever you want (not a big
deal in this case, but if you had several parameters going in you could still make _value be a single, simple int).
--
~Ethan~
More information about the Python-Dev
mailing list