Python±Ê¼Ç£¨10£©
PythonÖеÄÒì³£
µ±ÄãµÄ³ÌÐòÖгöÏÖijЩÒì³£µÄ×´¿öµÄʱºò£¬Òì³£¾Í·¢ÉúÁË¡£
Ò».´¦ÀíÒì³£
ÎÒÃÇ¿ÉÒÔʹÓÃtry..exceptÓï¾äÀ´´¦ÀíÒì³£¡£ÎÒÃǰÑͨ³£µÄÓï¾ä·ÅÔÚtry-¿éÖУ¬¶ø°ÑÎÒÃǵĴíÎó´¦ÀíÓï¾ä·ÅÔÚexcept-¿éÖС£
ÀýÈç:
#!/usr/bin/python
# Filename: try_except.py
import sys
try:
s = raw_input('Enter something --> ')
except EOFError:
print '\nWhy did you do an EOF on me?'
sys.exit() # exit the program
except:
print '\nSome error/exception occurred.'
# here, we are not exiting the program
print 'Done'
Êä³öΪ:
$ python try_except.py
Enter something -->
Why did you do an EOF on me?
$ python try_except.py
Enter something --> Python is exceptional!
Done
¶þ.Òý·¢Òì³£
Äã¿ÉÒÔʹÓÃraiseÓï¾äÒý·¢Òì³£¡£Ä㻹µÃÖ¸Ã÷´íÎó/Òì³£µÄÃû³ÆºÍ°éËæÒì³£´¥·¢µÄÒì³£¶ÔÏó¡£Äã¿ÉÒÔÒý·¢µÄ´íÎó»òÒì³£Ó¦¸Ã·Ö±ðÊÇÒ»¸öError»òExceptionÀàµÄÖ±½Ó»ò¼ä½Óµ¼³öÀà¡£
ÀýÈç:
#!/usr/bin/python
# Filename: raising.py
class ShortInputException(Exception):
'''A user-defined exception class.'''
def __init__(self, length, atleast):
Exception.__init__(self)
self.length = length
self.atleast = atleast
try:
s = raw_input('Enter something --> ')
if len(s) < 3:
raise ShortInputException(len(s), 3)
# Other work can continue as usual here
except EOFError:
print '\nWhy did you do an EOF on me?'
except ShortInputException, x:
print 'ShortInputException: The input was of length %d, \
was expecting at least %d' % (x.length, x.atleast)
else:
print 'No exception was raised.'
Êä³öΪ:
$ python raising.py
Enter something -->
Why did you do an EOF on me?
$ python raising.py
Enter something --> ab
ShortInputException: The input was of length 2, was expecting at least 3
$ python raising.py
Enter something --> abc
No exception was raised.
Èý.try..finally
¼ÙÈçÄãÔÚ¶ÁÒ»¸öÎļþµÄʱºò£¬Ï£ÍûÔÚÎÞÂÛÒì³£·¢ÉúÓë·ñµÄÇé¿ö϶¼¹Ø±ÕÎļþ£¬¸ÃÔõô×öÄØ£¿Õâ¿ÉÒÔʹÓÃfinally¿éÀ´Íê³É¡£×¢Ò⣬ÔÚÒ»¸
Ïà¹ØÎĵµ£º
ÔõôÕÒ²»µ½µÚÈýÕµÄѧϰ±Ê¼ÇÁË£¿¶ªÁË£¿
PythonµÄº¯ÊýûÓÐʲôµÄ£¬¿ÉÒÔ˵£¬¿´ÁË¡¶¼òÃ÷Python½Ì³Ì¡·ºó£¬¾Í»áдÁË¡£
ÕâÒ»ÕÂÌṩµÄÄÚÈÝÒ²±È¡¶¼òÃ÷Python½Ì³Ì¡·Òª¶àһЩ¡£±È½Ï¸´ÔÓµÄÊÇ×÷ÓÃÓò¹æÔò£¬²»ÖªµÀÊÇÊéû½²Çå³þ»¹ÊÇ·ÒëµÃ²»ºÃ£¬±È½ÏÄѶ®¡£Ç®Äܵġ¶C++³ÌÐò½Ì³Ì¡·¹ØÓÚº¯ÊýµÄ×÷ÓÃÓò¹æÔò½²µÃÒªÇå³þЩ£¬ÓÐC++µÄ֪ʶÔÚÀïÃæ£¬ ......
¹¤ÓûÉÆÆäʱØÏÈÀûÆäÆ÷£¡
¿ª·¢PythonÓÃʲô¹¤¾ßºÃÄØ£¿Æäʵ¸ÕѧPythonµÄ»°£¬Ê¹ÓÃIDLE¾Í¹»ÁË£¬ËäÈ»µ÷ÊÔ²»ÊÇÌØ±ð·½±ã£¬µ«ÊǶÔÓÚ³õѧÒѾ¹»ÁË£¬¿ÉÒÔʹÓÃPrint½øÐмòµ¥µÄµ÷ÊÔ£¬²»½¨ÒéʹÓüÇʱ¾½øÐпª·¢£¬²»ÖªµÀµÄÈÏΪÄãºÜÅ££¬ÖªµÀµÄ……ÕâÊÇ×Ô¼º¸ø×Ô¼ºÕÒ×ïÊÜ£¬ÓÃEditplusÃ²Ë ......
#---------------------תתתתתתתתתתתתתתתתתתתתתתת-------------------------------------------#
Python×÷ΪһÖÖ¹¦ÄÜÇ¿´óÇÒͨÓõıà³ÌÓïÑÔ¶ø¹ãÊÜºÃÆÀ£¬Ëü¾ßÓзdz£ÇåÎúµÄÓï·¨ÌØµã£¬ÊÊÓÃÓÚ¶àÖÖ²Ù×÷ϵͳ£¬Ä¿Ç°ÔÚ¹ú¼ÊÉϷdz£Á÷ÐУ¬ÕýÔڵõ½Ô½À´Ô½¶àµÄÓ¦Óá£
¡¡¡¡ÏÂÃæ¾ÍÈÃÎÒÃÇÒ»ÆðÀ´¿´¿´ËüµÄÇ¿´ó ......
żȻÐèÒªÓõ½ÕâÑùÒ»¸öº¯Êý£¬ÔÚDelphiÖУ¬ÓÐÏֳɵĺ¯Êý¿ÉÒÔµ÷Óã¡ÔÚpythonÖУ¬ÎÒÕÒÁ˰ëÌì¶¼»¹Ã»ÕÒµ½£¬×îºó×Ô¼ºÐ´ÁËÒ»¸öº¯ÊýÈçÏ£º
def dayOfMonth(date):
if date.month == 12:
return 31
else:
return (date.replace(month=date.month+1, day=1) - datetime.timedelta(days=1)).day
......
ÔÚ½²Êöfilter£¬mapºÍreduce֮ǰ£¬Ê×ÏȽéÉÜÒ»ÏÂÄäÃûº¯Êýlambda¡£
lambdaµÄʹÓ÷½·¨ÈçÏ£ºlambda [arg1[,arg2,arg3,...,argn]] : expression
ÀýÈ磺
>>> add = lambda x,y : x + y
>>> add ......