enum question

Larry Bates lbates at syscononline.com
Fri Mar 4 19:23:22 EST 2005


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



More information about the Python-list mailing list