enum question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • M.N.A.Smadi

    #1

    enum question

    does python support a C-like enum statement where one can define a
    variable with prespesified range of values?

    thanks
    m.smadi
  • Larry Bates

    #2
    Re: enum question

    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__(sel f, attr, x):
    if x in range(10): self.__dict__[attr]=x
    else:
    print "error, x not in ", str(range(10))
    [color=blue][color=green][color=darkred]
    >>> x=a()
    >>> x.value=11[/color][/color][/color]
    error, x not in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][color=blue][color=green][color=darkred]
    >>> x.value=8
    >>>[/color][/color][/color]

    Larry Bates


    M.N.A.Smadi wrote:[color=blue]
    > does python support a C-like enum statement where one can define a
    > variable with prespesified range of values?
    >
    > thanks
    > m.smadi[/color]

    Comment

    • Stephen Toledo-Brown

      #3
      Re: enum question

      M.N.A.Smadi wrote:
      [color=blue]
      > does python support a C-like enum statement where one can define a
      > variable with prespesified range of values?[/color]

      Not built in, but there are various solutions available, some simpler
      than others. See the Infrequently Asked Questions:


      --
      Steve Toledo-Brown
      Speaking for myself only.
      Humans please use domain uk.ibm.com

      Comment

      • Patrick Useldinger

        #4
        Re: enum question

        M.N.A.Smadi wrote:[color=blue]
        > does python support a C-like enum statement where one can define a
        > variable with prespesified range of values?
        >
        > thanks
        > m.smadi[/color]
        [color=blue][color=green][color=darkred]
        >>> BLUE, RED, GREEN = 1,5,8
        >>> BLUE[/color][/color][/color]
        1[color=blue][color=green][color=darkred]
        >>> RED[/color][/color][/color]
        5[color=blue][color=green][color=darkred]
        >>> GREEN[/color][/color][/color]
        8

        Comment

        • Carl Banks

          #5
          Re: enum question


          M.N.A.Smadi wrote:[color=blue]
          > does python support a C-like enum statement where one can define a
          > variable with prespesified range of values?[/color]

          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

          Comment

          Working...