Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19738272
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Python/Ruby

2009-10-26 14:54:51

#!/usr/bin/env python

"""This starts the python interpreter; captures the startup message; then gives
the user interactive control over the session. Why? For fun... """

# Don't do this unless you like being John Malkovich
# c = pexpect.spawn ('/usr/bin/env python ./python.py')

import pexpect
c = pexpect.spawn ('/usr/bin/env python')
c.expect ('>>>')
print 'And now for something completely different...'
f = lambda s:s and f(s[1:])+s[0] # Makes a function to reverse a string.
print f(c.before)
print 'Yes, it\'s python, but it\'s backwards.'
print
print "Escape character is '^]'."
print c.after,
c.interact()
c.kill(1)
print 'is alive:', c.isalive()

需要注意的地方:

f = lambda s:s and f(s[1:])+s[0] # Makes a function to reverse a string.

lambda是用来定义简短函数的一种办法,注意这里还使用了递归,巧妙地利用and来判断递归的结束。

本程序执行过程中可能会报I/O错误。


阅读(3903) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~