[Python-Dev] once [was: Simple Switch statementZ]

Ron Adam rrr at ronadam.com
Thu Jun 29 19:22:25 CEST 2006


Christos Georgiou wrote:
> I haven't followed the complete discussion about once, but I would assume it 
> would be used as such:
> 
> once <name> = <expression>
> 
> that is, always an assignment, with the value stored as a cellvar, perhaps, 
> on first execution 0f the code.
> 
> Typically I would use it as:
> 
> def function(a):
>     once pathjoin = os.path.join
>     <etc>


In the "name = (once expr)" form I gave, the property of a constant name 
that can't be rebound or that of a value that persists across function 
call invocations isn't needed.  I was trying to separate the different 
behaviors cleanly and clearly.

     # once as constant assignment and skipped line later.
     for n in range(x, 10):
         once startcube x**3     # assigns constant value, skips later
         print startcube
         startcube += 1          # give an exception

So this is the same as "const startcube x**3", except it's ignored if it 
is executed again instead of giving an excepton.


Here the constantness property isn't needed.

     # once as calc once, use result many times expression.
     for n in range(x, 10):
         startcube = (once x**3)     # calculated once used many
         print startcube
         startcube += 1              # Ok to do this


I wasn't suggesting which behavior (or combination of) is correct.  That 
would depend on what problem is meant to solved.

A fourth property of external has been touched on in these threads where 
  some of the suggestions require doing a calculation on a yet to be 
known value. That's usually handled by linkers in other languages and 
probably isn't something desired in a dynamic language like Python.


Cheers,
    Ron

* I may not be able to reply, do to leaving on a trip.  Already should 
be gone. ;-)





More information about the Python-Dev mailing list