New statement proposal for Python (fwd)

Lulu of the Lotus-Eaters mertz at gnosis.cx
Thu Jun 14 21:10:30 CEST 2001


David LeBlanc <whisper at oz.nospamnet> wrote:
|One thing that bugs me about Python is that there's no really good way to
|have named constants so that magic numbers can be avoided. Assigning to a
|variable that (should) never changes is not the same (i.e. the "should
|never" part).

One way to solve the "-should- never change" thing is to use the Xoltar
Toolkit's functional module, and its 'Bindings' class.  Of course, I
happened to have contributed that class to Bryn Keller, so I may be
biased... and moreover, I happened to have written an article that, in
part, discusses this:
<http://www-106.ibm.com/developerworks/linux/library/l-prog2.html>) :-).

Here's an example from the article:

  Listing 2. Python FP session with guarded rebinding
  ------------------------------------------------------------------------
  >>> from functional import *
  >>> let = Bindings()
  >>> let.car = lambda lst: lst[0]
  >>> let.car = lambda lst: lst[2]
  Traceback (innermost last):
    File "<stdin>", line 1, in ?
    File "d:\tools\functional.py", line 976, in __setattr__
      raise BindingError, "Binding '%s' cannot be modified." % name
  functional.BindingError:  Binding 'car' cannot be modified.
  >>> car(range(10))
  0

An even shorter spelling is:

  _= Bindings()
  _.SPAM = 'eggs'
  myvar = _.SPAM + "more eggs"

HTH.

Yours, Lulu...




More information about the Python-list mailing list