Guide to the python interp. source?

Huaiyu Zhu huaiyu at gauss.almadan.ibm.com
Mon Jul 29 14:32:37 EDT 2002


Tim Gahnström /Bladerman <tim at bladerman.com> wrote:
>
>"Terry Reedy" <tjreedy at udel.edu>
>> > Things I want to change is for example, everything should be "call
>> > refferense",
>>
>> It already is, which is why functions can mutate mutable objects.
>> (This surprises some beginners.)  Of course, for immutables (numbers,
>> strings, tuples), there is no semantic difference between reference
>> and value passing except for reduced memory usage.
>
>I want to make all variables mutable. When you want to make functions like
>inc(x) where inc is supposed to increase the value of x by one. How is that
>done in Python?
>That is my view of CBR, maybe that is not the correct definition and I don't
>need CBR.

It's not a matter of how you can do it, but what you really want to do.
Consider this:

x = y = 3          # (1)
inc(x)             # (2)
x == 3 and y == 3  # (3)
x == 4 and y == 3  # (4)
x == 4 and y == 4  # (5)

Let's see what constraints you have for the semantics of inc.

- After (1), both x and y are references to an object named 3.  

- Whatever you want your new language to be, you do not want the object
  named 3 to actually have a value of 4.  So you do not want to change the
  object referenced by x.

- You can change the name x so that it now points to the object named 4.
  You can keep the change in the function inc, so that (3) is true (as
  Python currently does), or let it affect the calling scope, so that (4) is
  true (as you might want to do).  Either way (5) is false because y is a
  separate name.

- To make (5) true, there must be a commont "sign post" to which both x and
  y can refer to, and which can be changed from pointing to the object named
  3 to the object named 4.  This would be just a mutable int.  It can be
  implemented in existing Python similar to UserDict and UserList.  You
  would need change (1) to x = y = UserInt(3) to use it.  Keep in mind that
  you do not want the object named 3 itself to be mutable.  Otherwise you
  are in for a big surprise next time you do z = 3.

I guess what you want is (4), ie to make inc(x) change the reference of x in
the calling scope.  In other words, you want to make the formal arguments of
a function an alias of the actual arguments.  In that case these arguments
are really in a "tunneled" scope, accessible from both sides of a function
call.  It would behave very differently from existing Python.  For example:

def remove(x): del x
s = 3
remove(s)
s  # NameError: name 's' is not defined

It might even be more powerful than Python in the sense that many statements
and syntax structures can be replaced by functions.  Since functions are
first class objects you can manipulate them in program, thereby construct
program structures without using eval.  This has a Lisp feeling.  If you
decide that this is indeed what you want to do, other people might give you
some idea on how easy/difficult it is and where to start.

Huaiyu



More information about the Python-list mailing list