Python入门的36个例子 之 34
源代码下载:下载地址在这里
raise有两个参数,第一个是由我们自己定义的异常类型,第二个是关于此异常的少量说明信息。
# 038
def getAge():
age = input('Input your age:')
if (age < 0 or age > 160):
raise 'BadAgeError', 'It is impossible!!!!!'
# end of if
return age
# end of def
print getAge()
output:
以下是两次运行的结构
>>>
Input your age:22
22
>>>
Input your age:911
Traceback (most recent call last):
File "C:/Documents and Settings/ning/桌面/Python Intro/038_RaiseAnError.py", line 11, in <module>
print getAge()
File "C:/Documents and Settings/ning/桌面/Python Intro/038_RaiseAnError.py", line 6, in getAge
raise 'BadAgeError', 'It is impossible!!!!!'
TypeError: exceptions must be classes or instances, not str
>>>
相关文档:
代码很简单,不到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 ......
源代码下载:下载地址在这里
# 023
# Tuple(元素组)是不可变的列表
tuple1 = (1, 2, 3)
print tuple1
tuple2 = (tuple1, 4, 5, 6) # 一个元素组可以作为另外一个元素组的元素
print tuple2 # 并且能够在存储的时候保持原始的逻辑关系
for ele in tuple2:
print ele
print '\n'
for ele in tuple2[0]:
pr ......
源代码下载:下载地址在这里
e.g.1
# 030
aFile = file(r'C:\temp.txt', 'a')
aFile.write('又添加了一行。')
aFile.close()
output:
e.g.2
# 030
aFile = file(r'C:\temp.txt', 'a')
aFile.write('又添加了一行。')
aFile.close()
output:
e.g.3
实现根据原始文件有没有最后一行空行的情况来进行&ldqu ......
源代码下载:下载地址在这里
# 035
class Person:
population = 0 #这个变量是属于整个类的
def __init__(self, name):
self.name = name
print '初始化 %s' % self.name
Person.population += 1
# end of def
def __del__(self):
print '%s says bye.' % self. ......
源代码下载:下载地址在这里
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 ......