Why is Python popular, while Lisp and Scheme aren't?
Martti Halminen
martti.halminen at kolumbus.fi
Thu Nov 14 17:04:29 EST 2002
Donn Cave wrote:
>
> Quoth Michael Hudson <mwh at python.net>:
> ...
> | Consider this code from Python's getpass module:
> |
> | old = termios.tcgetattr(fd) # a copy to save
> | new = old[:]
> |
> | new[3] = new[3] & ~termios.ECHO # 3 == 'lflags'
> | try:
> | termios.tcsetattr(fd, termios.TCSADRAIN, new)
> | passwd = _raw_input(prompt)
> | finally:
> | termios.tcsetattr(fd, termios.TCSADRAIN, old)
> |
> | In CL if you wanted to do this sort of thing often you'd write
> | something like (making up interface names as I go):
> |
> | (defmacro with-invisible-input (fd &body body)
> | (let ((old-state (gensym))
> | (new-state (gensym)
> | (fd-var (gensym)))
> | `(let* ((,old-state (termios:tcgetattr ,fd))
> | (,new-state (termios:copy-state ,old-state))
> | (,fd-var ,fd))
> | (setf (termios:lflags ,new-state)
> | (logior (termios:lflags ,new-state) termios:echo))
> | (termios:tcsetattr ,fd-var ,new-state)
> | (unwind-protect
> | ,body
> | (termios:tcsetattr ,fd-var ,old-state))))))
> |
> | which isn't exactly pretty, but now the above Python becomes:
> |
> | (with-invisible-input
> | (funcall input-func prompt))
> |
> | which *is* better.
>
> I don't get it - how is this different from an ordinary function?
> Like,
>
> def invisibly(tty, fun):
> ... termios stuff
> try:
> result = fun()
> finally:
> ... termios stuff
> return result
>
> def getpass(prompt):
> def myrawread(prompt=prompt):
> return _raw_read(prompt)
> return invisibly(0, myrawread)
>
> I was thinking that with- was going to be a Pascal-like thing where
> some class instance scope was made `local'.
This may have been a little too restricted example. The real strength of
the macro approach (at least if you used , at body instead of ,body in the
macro) is that you are not restricted to a single function call, the
body may be an arbitrarily long piece of program code.
--
More information about the Python-list
mailing list