variable declaration

Christian Dieterich cdieterich at geosci.uchicago.edu
Tue Feb 1 16:17:04 EST 2005


On Dé Máirt, Feabh 1, 2005, at 12:19 America/Chicago, Alex Martelli 
wrote:

> Michael Tobis <mt at 3planes.com> wrote:
>    ...
>> I don't know that it's ever necessary to rebind, but it is, in fact,
>> common, and perhaps too easy. In numeric Python, avoiding rebinding
>> turns out to be a nontrivial skill.
>
> Well, a for-statement is BASED on rebinding, for example.  Maybe you
> don't mean to address rebinding per se, but rebinding in ``separate
> statements''?  The ``separation'' needs to be defined carefully to make
> while-loops work, too.  Normally, a while-statement's header clause
> would be something like:
>     while <expression>:
> where the expression depends on the values bound to some local
> variables.  For the first evaluation, the variables need to be bound by
> earlier statements; for the expression to eventually become false, it's
> likely (unless we're talking about mutable objects) that the variables
> need to be re-bound in the loop body.
>
> For example, consider:
>
> def f(a, b):
>     x = 0
>     while x < 100000:
>         print x,
>         x = a*x + b
>     print

I think you guys are talking about rebinding of quite different 
objects. Rebinding x in the above example is not what Michael Tobis is 
talking about, I presume, though x is prone to the 'epselon' error. 
But, C'est la vie.

If x was some sort of counter (time step, iteration) and there was a 
Numeric array y to be processed in that loop,

def f(a, b):
   y = Numeric.zeros((100, 100), 'd')
   x = 0
   while x < 100000:
     x = a*x + b
     y = a*y + b

it is actually important (e.g. in terms of performance) not to rebind 
y. Okay, in this case it is straightforward to change function f to 
function g, so that y does not get recreated (rebound).

def g(a, b):
   y = Numeric.zeros((100, 100), 'd')
   x = 0
   while x < 100000:
     x = a*x + b
     y *= a
     y += b

I don't know if y gets 'rebound' in a strict sense of the word. But y 
does not call it's constructor (which is expensive). And in terms of 
performance
y = Numeric.zeros((100, 100), 'd')
         1    0.010    0.010  574.050  574.050 profile:0(f(1,1))
         1   -0.000   -0.000  416.640  416.640 profile:0(g(1,1))
   y = numarray.zeros((100, 100), 'd')
         1    0.020    0.020  231.359  231.359 profile:0(f(1,1))
         1    0.010    0.010   27.899   27.899 profile:0(g(1,1))
it's quite a difference.

> would you require the programmer to find out a closed-form expression
> for this recurrence relation, in order to avoid having to rebind the
> name 'x'?  OK, in this particular case it may be OK, if you are willing

Using Numeric arrays or Numarrays, it might help to have some 
closed-form expression for the above (I'm quite sure there is, see 
daxpy in BLAS). But then, I'd say it's in the responsability of the 
programmer who did class Y to create y to prevent the user from using y 
= a*y + b. Either in the documentation or really strictly by not 
implementing + and * (only =+ and =*). This kind of 
restriction/declaration should not be in Python, but in the module that 
provides Y.

> to put competence in college-level algebra as a prerequisite for using
> Python.  But what if the rebinding in the body of the loop was to a 
> more

I believe those using Numeric/Numarray might have heard of algebra and 
in-place operations. So, one could even argue that it's up to the user 
to find an efficient way to avoid rebinding of y.

Christian




More information about the Python-list mailing list