PEP 354: Enumerations in Python
Felipe Almeida Lessa
felipe.lessa at gmail.com
Tue Feb 28 21:34:21 EST 2006
Em Seg, 2006-02-27 às 17:10 -0800, Paul Rubin escreveu:
> Ben Finney <bignose+hates-spam at benfinney.id.au> writes:
> > If an enumeration object were to be derived from, I would think it
> > just as likely to want to have *fewer* values in the derived
> > enumeration. Subclassing would not appear to offer a simple way to do
> > that.
>
> pentium_instructions = enum('add', 'sub', 'mul', ) # etc
>
> athlon64_instructions = enum('add64', 'sub64', # etc
> base_enum=pentium_instructions)
>
> # 386 has no floating point unit
> i386_instructions = enum(base_enum=pentium_instructions,
> remove=('addf', 'subf', 'mulf',)) # etc
Or maybe just...
i386_instructions = enum('add', 'sub', 'mul', ...)
pentium_instructions = enum(i386_instructions, 'addf', 'subf',
'mulf', ...)
athlon64_instructions = enum(pentium_instructions, 'add64',
'sub64', ...)
myprocessor_instructions = enum('foo', 'bar', 'baz', ...)
all_instructions = enum(athlon64_instructions,
myprocessor_instructions)
...and it could infer from the type that it's another enum to be
included. Also...
(i386_instructions.add == pentium_instructions.add ==
athlon64_instructions.add == all_instructions.add) == True
...and so on.
--
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."
-- Sun Tzu, em "A arte da guerra"
More information about the Python-list
mailing list