Pass variable by reference

Chris Angelico rosuav at gmail.com
Tue May 6 19:46:16 EDT 2014


On Wed, May 7, 2014 at 7:00 AM, Mark H Harris <harrismh777 at gmail.com> wrote:
> What does the word "variable" mean. Think BASIC variables. You can set them,
> you can reset them, you can delete them, you can change them.  ... because
> they are variable.
>
> Python has names bound to objects... some of which you may not change. Once
> the name is bound to an object you may bind it to another object, but you
> may not change it, nor may you change the object it is bound to if the
> object is immutable.

Is this code mutating or rebinding?

x = 1.1
y = 2.2
x = x + y

What language did I write that in? Is there really a fundamental
difference between languages in which that is equally valid syntax and
does exactly the same thing? Apart from the fact that BASIC defaults
to single-precision float (in the absence of a hash suffix), Python
uses double-precision, and REXX uses decimal strings, this sequence
will work identically in all of them. Does BASIC have variables?
Presumably, since you can pass them by reference. Does REXX? You can't
pass by reference (all you can do is EXPOSE, which is more like
scoping rules, only it isn't); a C function can fiddle with any named
variable in its caller's scope, so a common way to "return" multiple
values is to pass the name of a stem (which isn't an array, and isn't
a dict/mapping, and isn't really an object at all) into which the
results will be placed. Python has objects and names, and changes
which object a name is bound to.

The mechanics differ, but fundamentally, x started out as 1.1 and
ended up as 3.3. That means x varied in value, and is thus a variable.
The differences are relatively insignificant.

ChrisA



More information about the Python-list mailing list