Python Learning Series (9) (IO and Exception Handling) and pythonio
Python Learning Series (9) (IO and Exception Handling)
Python Learning Series (8) (Object-Oriented basics)
I. Storage
1. Python provides a standard module called pickle, which can store any Python object in a file and obtain it completely, this is called a persistent storage object. Similarly, cPickle, a module with the same functions, is written in C language and 1000 times faster than pickle.
2. Example:
1 import cPickle as p 2 shoplistfile = 'shoplist. data '3 shoplist = ['apple', 'mango', 'carrot'] 4 f = file (shoplistfile, 'w') 5 p. dump (shoplist, f) 6 f. close () 7 del shoplist 8 f = file (shoplistfile) 9 storedlist = p. load (f) 10 print storedlistView Code
To store an object in a file, first open a file object in write mode, and then call the dump function of the storage module to store the object to the open file. This process is called storage.
Use the load function of the pickle module to retrieve objects. This process is called storage.
2. Exceptions
1. Use try:
1 import sys2 m = raw_input ('enter a float: ') 3 n = raw_input ('enter a float:') 4 try: 5 print float (m)/float (n) 6 bytes T: 7 print 'divisor cannot be 0! '8 sys. exit ()View Code
2. An exception is thrown using the raise statement:
1 class shortInput (Exception): 2 def _ init _ (self, length, atleast): 3 Exception. _ init _ (self) 4 self. length = length 5 self. atleast = atleast 6 try: 7 s = raw_input ('enter something --> ') 8 if len (s) <3: 9 raise shortInput (len (s), 3) 10 TB t EOFError: 11 print '\ nWhy did you do an EOF on me? '12 bytes t shortInput, x: 13 print 'The input was of length % d, was Sort Ting at least % d' % (x. length, x. atleast) 14 else: 15 print 'no exception was raised! 'View Code
3. Use try... Finally:
1 import time 2 try: 3 f1_file('format.txt ') 4 while True: 5 line = f. readline () 6 if len (line) = 0: 7 break 8 time. sleep (2) 9 print line10 finally: 11 f. close () 12 print 'cleaning up ...... Closed the file! 'View Code
Iii. Advanced content
1. Special Methods
| Name |
Description |
| _ Init _ (self ,......) |
Called before the newly created object is returned for use. |
| _ Del _ (self) |
It is called just before the object is deleted. |
| _ Lt _ (self, other) |
It is called when the value is smaller than the operator. Similarly, all operators have special methods. |
| _ Str _ (self) |
It is called when the print statement is used for an object or str () is used. |
| _ Getitem _ (self, key) |
It is called when the x [key] index operator is used. |
| _ Len _ (self) |
Call a sequence object when using the len function. |
2. lambda expressions
Lambda statements are used to create a new function object and return it at runtime.
1 def make_repeater (n): 2 return lambda s: s * n 3 4 twice = make_repeater (2) 5 6 print twice ('word') 7 print twice (2) 8 >>==================================== RESTART ==== =============================9 >>> 10 wordword11 4View Code3, exec, and eval statements exec statements are used to execute Python statements stored in strings or files. The eval statement is used to calculate the valid Python expression stored in the string. 1 >>> exec '''print \ 'Hello world \ ''' 2 Hello world3 >>> eval ('2 * 3') 4 6View Code4, assert statement: Used to declare that a condition is true. 5. repr function: used to obtain the standard string representation of an object. This function can also be completed by quotation marks. 1 >>> I = [] 2 >>> I. append ('item') 3 >>> i4 ['item'] 5 >>> repr (I) 6 "['item']" 7 >>>View Code
Iv. Summary
In this chapter, I have initially learned about Exception Handling. Regarding the importance of exception handling, I believe that Mr. mainiao knows the importance of exception handling. As a beginner, I 'd like to add more details.