Pointers to variables

Randall Hopper aa8vb at vislab.epa.gov
Fri Apr 23 08:37:14 EDT 1999


Fredrik Lundh:
 |> but this one doesn't:
 |> 
 |>     (3)  min = 0
 |>          max = 0
 |>          for ( var, val ) in [( min,   1 ), ( max, 100 )]:
 |>            var = val
 |
 |here, you tell python to change the binding for the name "var"
 |in the local name space so it points to the same thing as the
 |name "val".

Ahh (light bulb goes on).  Makes sense.  I should have seen that (need my
morning caffeine).

When the "for" iterates, var is really an alias for min and max's storage
-- what you want.  But the problem is you can't "change" the value of
storage with assignment to var (that just rebinds var).

And Python being "pointerless" there is no:

     *var = val
or:
     memcpy( var, val, sizeof(*var) )

-like construct.  So you have to resort to other means (setattr).

 |> So basically this is just a little asymmetry in the language.  Putting a
 |> variable in a list/tuple (valueof(var)) performs a shallow copy rather than
 |> a deep copy.
 |
 |nope.  content is never copied.  only references.  all the time.
 |perfect symmetry.

Thanks.

Randall




More information about the Python-list mailing list