Re: enum question

2005-03-09 Thread Terry Hancock
On Friday 04 March 2005 02:54 pm, M.N.A.Smadi wrote:
 does python support a C-like enum statement where one can define a 
 variable with prespesified range of values?

No, but in most situations where I would've used an enum, I use a 
trivial class like this:

class states:
ON, OFF, UNKNOWN, UNDEFINED, INCOMPREHENSIBLE, SILLY = range(6)

The values are actually integers of course.  Then I can pass named values to a 
function:

from silly import walk, states

walk(kind=states.SILLY)

If you want to make them more like a bitfield, you could always do this instead:

class properties:
AUDIBLE, VISIBLE, SPEAKABLE = [2**i for i in range(3)]

then you can have, e.g.:

evil(allowed=False, AUDIBLE|VISIBLE|SPEAKABLE)

if mode  AUDIBLE: hear()
if mode  VISIBLE: see()

etc.

Obviously if you want more rigid assurance of the behavior of the variable
in question you'd need more work. But most of the time, enums are just to
make the code more readable, so I do this, which is extremely compact,
and yet pretty clear.  You *can* drop the enum values into the module
namespace, but then importing them is a pain, so I like to qualify them
with the class as shown.

Cheers,
Terry


-- 
--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks  http://www.anansispaceworks.com

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: enum question

2005-03-05 Thread Stephen Toledo-Brown
M.N.A.Smadi wrote:
does python support a C-like enum statement where one can define a 
variable with prespesified range of values?
Not built in, but there are various solutions available, some simpler 
than others. See the Infrequently Asked Questions: 
http://www.norvig.com/python-iaq.html

--
Steve Toledo-Brown
Speaking for myself only.
Humans please use domain uk.ibm.com
--
http://mail.python.org/mailman/listinfo/python-list


Re: enum question

2005-03-05 Thread Patrick Useldinger
M.N.A.Smadi wrote:
does python support a C-like enum statement where one can define a 
variable with prespesified range of values?

thanks
m.smadi
 BLUE, RED, GREEN = 1,5,8
 BLUE
1
 RED
5
 GREEN
8
--
http://mail.python.org/mailman/listinfo/python-list


Re: enum question

2005-03-05 Thread Carl Banks

M.N.A.Smadi wrote:
 does python support a C-like enum statement where one can define a
 variable with prespesified range of values?

The thing is, variables don't have types; objects do.  A variable can
be bound to an object of any type, so there's no way to prespecify a
range of values for a variable.

Your question has the air of someone who's evaluating Python features,
considering whether to try it.  If so, you might need to widen your
worldview a little to understand Python; its variables are
fundamentally different from C, and things like enums don't make much
sense in Python because of it.  However, Python is versatile enough
that you can get something to that effect if you really need it.


-- 
CARL BANKS

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: enum question

2005-03-04 Thread Larry Bates
Not completely sure I understand (my C is weak).
But you can do:

x=range(9)

x's contents will be
[0,1,2,3,4,5,6,7,8]

or

x=range(12,25)

x's conetnst will be
[12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]

if you mean a way to limit x's contents to a predefined
list of values, you need to write a class for that and
override the __setattr__ method to limit.  Quick
example:

class a:
def __setattr__(self, attr, x):
if x in range(10): self.__dict__[attr]=x
else:
print error, x not in , str(range(10))

 x=a()
 x.value=11
error, x not in  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 x.value=8


Larry Bates


M.N.A.Smadi wrote:
 does python support a C-like enum statement where one can define a
 variable with prespesified range of values?
 
 thanks
 m.smadi
-- 
http://mail.python.org/mailman/listinfo/python-list