Pascal int()

Jason Stokes jstok at bluedog.apana.org.au
Sat Mar 18 15:04:10 CET 2000


Michal Bozon wrote in message ...

>But this is not exactly what I want. I want tou use in function a variable
>itself, not its name in quotes. I know that it is not simple, but I think
>that there should be a better way than that you suggersted.


I'm afraid Python's design militates against this style.  I will attempt to
explain:

In Python, "variables" are merely names that are bound to objects.  The
objects themselves may be mutable or immutable.  In the case of numbers,
they are immutable.

"Changing the value of a variable", therefore, really involves binding a
name to a new object.  This is quite different from, eg, C where an integer
is a cell of memory that can have its value changed.  In Python, the value
of integer objects cannot be changed.

This means that without playing tricks with namespace dictionaries, the best
you can do is:

def inc(x);
    return x+1 # returns a *new* integer object one greater than x

a = 1
a = inc(a) # rebinds a to the integer object storing the value "2"

If, like Smalltalk or Lisp, there was a syntax for passing a name or symbol
as an argument instead of a reference to an object, it would be possible to
write a Pascal style inc() function.  Unfortunately despite the previously
posted mechanism, which is brittle and breaks for non-global names, I cannot
think how this would be implemented naturally in Python as it stands,
although a Python god may correct me.






More information about the Python-list mailing list