Python入门的36个例子 之 33
源代码下载:下载地址在这里
# 037
fileName = ''
while 1:
fileName = raw_input("Input a file name:")
if fileName == 'q':
break
try:
f = file(fileName, 'r')
print 'Opened a file.'
except:
print 'There is no file named', fileName
# end of try and except
f.close()
# end of while
output:
Input a file name:haha
There is no file named haha
Input a file name:txt
There is no file named txt
Input a file name:1
There is no file named 1
Input a file name:037_Exception.py
Opened a file.
Input a file name:q
相关文档:
代码很简单,不到5k行。但是思路挺好的,改成non-blocking了之后效率就是能提高不少,特别是考虑到现代的web app都需要和其他的
HTTP服务器通信,blocking的代价太大了。 Tornado is an open source version of the scalable, non-blocking web server and tools that power FriendFeed. The FriendFeed application ......
源代码下载:下载地址在这里
# 025
# 序列的神奇之处在于你可以使用相同的方式tuple、list和string
se = ['a', 'b', 'c', 'd']
li = ['a', 'b', 'c', 'd']
tu = ('a', 'b', 'c', 'd')
string = 'abcd'
print se[1]
print li[1]
print tu[1]
print string[1]
# 序列的另外一个神奇之处在于,你可以使用负数进行索 ......
源代码下载:下载地址在这里
# 033
class Person:
age = 22
def sayHello(self): # 方法与函数的区别在于需要一个self参数
print 'Hello!'
# end of def
# end of class # 这是我良好的编程风格
p = Person()
print p.age
p.sayHello()
output:
22
Hello! ......
源代码下载:下载地址在这里
# 034
class Person:
def __init__(self, name):
self.name = name # 这一句中第一个name是类中包含的域,第二个name是传进来的参数
# end of def
def sayHello(self):
print 'Hello!'
# end of def
# end of class
p = Person('Ning')
print p.nam ......
源代码下载:下载地址在这里
A Byte Of Python
中关于继承有这样的文字:
Suppose you want to write a program which has to keep track of the
teachers and students in a college. They have some common
characteristics such as name, age and address. They also have specific
characteristics such as sala ......