Proposal for adding symbols within Python

Ben Finney bignose+hates-spam at benfinney.id.au
Mon Nov 14 16:08:20 EST 2005


Pierre Barbier de Reuille <pierre_dot_barbier at _nospam_cirad.fr> wrote:
> The problem is not about having something constant !
> The main point with symbols is to get human-readable values.
> Let say you have a symbol "opened" and a symbol "closed". The state
> of a file may be one of the two.

    from some_enum_module import Enum

    FileState = Enum('open', 'closed')

    input_file.state = FileState.closed

> If you have these symbols, you can ask for the state at any point
> and get something readable. If you use constants valued, typically,
> to integers, the state of your file will we 0 or 1, which does not
> mean anything.

    str(input_file.state)  # -> 'closed'

> Now, if you're using an object with more than two states, and
> moreover if the number of states is likely to increase during
> developpement, it's much more convenient to directly get the
> *meaning* of the value rather than the value itself (which does not
> mean anything).

    PixelColour = Enum('red', 'green', 'blue', 'black')

> The key point that, I think, you misunderstand is that symbols are
> not *variables* they are *values*.

So far, I see nothing that requires anything but a special object type
with the behaviour you describe. Which most of the enumerated-type
implementations do quite neatly.

> Well, once more, new syntax for new objects never solve new
> problems, they just make them easier to write.

If you want to promote something, it would be best to implement it and
demonstrate some problems that it solves. You don't seem to be arguing
against a new object type, so perhaps it would be best to simply start
using that type to solve some actual problems.

Since "promotion" is the only argument you've given for new syntax
for this concept, I don't see what is served talking about creating
syntax for something that does not yet exist to be promoted. Once an
implementation exists for examination and is actually useful to some
amount of users for solving actual problems, that's the time to talk
about promoting it.

-- 
 \     "I went to the cinema, it said 'Adults: $5.00, Children $2.50'. |
  `\       So I said 'Give me two boys and a girl.'"  -- Steven Wright |
_o__)                                                                  |
Ben Finney



More information about the Python-list mailing list