Pascal int()

Gareth McCaughan Gareth.McCaughan at pobox.com
Sat Mar 18 19:57:56 EST 2000


Jason Stokes wrote:

> 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.

I'm no Smalltalk expert, but I don't think there's any way
to write a Lisp function such that

  (let ((a 1))
    (try-to-increment a)
    a)

will return anything other than 1. Or even

  (let ((a 1))
    (try-to-increment 'a)
    a)

which "quotes" A and passes the symbol instead of its value.

You can, of course, get this behaviour with macros. (Lisp's
macro system is extremely powerful -- much more so than C's,
for instance. It makes it possible to extend the language
in remarkable ways. Not that the power of the Lisp macro
system would really be stretched by an "increment this variable"
macro. And Lisp already has an "increment" macro anyway.)

>                                       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.

I'm not a deity of any sort, but if you don't mind quoting
then it's possible to implement an "increment" function in
Python that doesn't break for non-global names. But it's
horrible.

(You raise and catch an exception, look inside the traceback
object you can get from sys.exc_info, find the caller of the
function, and look in its "locals" dictionary. Bletch.)

The point that several people have made stands: the right
way to add 1 to the value of the variable "a" in Python is

    a = a+1

and there's no real need for an "inc" "function".

-- 
Gareth McCaughan  Gareth.McCaughan at pobox.com
sig under construction



More information about the Python-list mailing list