Why is Python popular, while Lisp and Scheme aren't?
Donn Cave
donn at u.washington.edu
Thu Nov 14 12:29:24 EST 2002
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'.
Donn Cave, donn at u.washington.edu
More information about the Python-list
mailing list