Python Learning Series (ix) (IO and exception handling)
Python Learning Series (eight) (object-oriented basis)
One, memory
1,python provides a standard module, called Pickle, that uses it to store any Python object in a file or to take it out completely, which is called a persistent storage object . Similarly, there is a module with the same function-cpickle, written in C, the speed is 1000 times times faster than the pickle.
2, Example:
1 ImportCpickle as P2shoplistfile='Shoplist.data'3shoplist=['Apple','Mango','Carrot']4F=file (Shoplistfile,'W')5 p.dump (shoplist,f)6 f.close ()7 delshoplist8f=file (shoplistfile)9storedlist=p.load (f)Ten PrintStoredlist
View 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 in an open file. This process is called storage.
Use the load function of the Pickle module to retrieve the object, which is called storage .
Two, exception
1, using try:
1 ImportSYS2M=raw_input ('Enter a float:')3N=raw_input ('Enter a float:')4 Try:5 PrintFloat (m)/float (n)6 except:7 Print 'the divisor cannot be 0! '8Sys.exit ()
View Code
2, throw an exception using the raise statement:
1 classShortinput (Exception):2 def __init__(self,length,atleast):3Exception.__init__(self)4Self.length=length5self.atleast=atleast6 Try:7S=raw_input ('Enter sth-->')8 ifLen (s) <3:9 RaiseShortinput (Len (s), 3)Ten exceptEoferror: One Print '\nwhy did you do a EOF on me?' A exceptshortinput,x: - Print 'The input was of length%d,was excepting at least%d'%(x.length,x.atleast) - Else: the Print 'No exception was raised!'
View Code
3, using try...finally:
1 Import Time2 Try:3F=file ('Format.txt')4 whileTrue:5Line=F.readline ()6 ifLen (line) = =0:7 Break8Time.sleep (2)9 Print LineTen finally: One f.close () A Print 'Cleaning up......closed the file!'
View Code
Third, advanced content
1, Special method
Name |
Description |
__init__ (self,......) |
Called before the new object is to be returned for use. |
__del__ (self) |
Called exactly before the object is to be deleted. |
__lt__ (Self,other) |
Called when the less-than operator is used. Similarly, there are special methods for all operators. |
__str__ (self) |
Called when the object is using the PRINT statement or using STR (). |
__getitem__ (Self,key) |
Called when using the X[key] index operator. |
__len__ (self) |
Called when the Len function is used on a Sequence object. |
2,lambda expression
The lambda statement is used to create a new function object and is returned at run time.
1 defmake_repeater (n):2 return Lambdas:s*N3 4Twice=make_repeater (2)5 6 PrintTwice ('Word')7 PrintTwice (2)8>>> ================================ RESTART ================================9>>>Ten Wordword One4
View Code3,exec and Eval statementsthe EXEC statement is used to execute a python statement stored in a string or file. the eval statement is used to calculate a valid python expression stored in a string.
1 exec " " print \ ' Hello world\ ' " " 2 Hello World 3 >>> eval ('2*3')4 6
View Code4,assert statement: Used to declare a condition to be true. 5,repr function: Used to get the canonical string representation of an object. This function can also be done with anti-quotes .
1 >>> i= [] ' 7 >>>
View Code
Four, summary
This chapter is the primary study of exception handling, about the importance of exception handling, I believe that every yard Mr. Nong know its importance, as a beginner, slowly add it.
Python Learning Series (ix) (IO and exception handling)