Include the following directories:
- Chapter 14th Documents
- Chapter 15th Class and Object
- 16th Chapter classes and functions
- 17th Chapter Classes and methods
- 18th Chapter Succession
Chapter 14th Documents
1. Reading and writing
A file needs to be opened before it is read and written, and finally the open file is closed.
#Read the file>>> f = open ('Words.txt')>>>PrintF<open file'Words.txt', mode'R'At 0x7fd74378e4b0>>>>F.readline ()'aa\r\n'>>>f.close ()#Write a file>>> f = open ('Sb.txt','W')>>>PrintF<open file'Sb.txt', mode'W'at 0x7fd74378e540>>>> line ="Hello, world!\n.">>>F.write (line)>>>f.close ()>>> exit ()
Note: To write a file, you need to open it using the ' W ' mode as a second argument. If the file already exists, then use write mode to open the old data will be erased and re-shape, so pay special attention ah. If the file does not exist, create a new one. In addition, the write parameter is a string that must be.
2, format operator
It is similar to the formatted string inside the C language in printf (), and can be formatted with output. %d is used to format integers,%g for formatting decimals, and%s for formatting strings.
When there is a sequence of formats in a string,
Print ' I have%d pens ' %Pens
When there is more than one sequence of characters in a string, the tenth operand must be a tuple. And each format sequence is grouped by an element in the corresponding tuple.
Print ' In %d years I has spotted%g%s. ' ' Camels ' 3 years I have spotted 0.1 camels.
3. File name and path
The OS module provides functions for manipulating files and directories to see the relevant functions of the module.
4. Catching exceptions
You can use a try statement to catch an exception, as in the following example:
Try : = Open ('1123.d') print' opened successfully ' fin.close ()except: print' did not open successfully, may file does not exist \ n'
5. Database: module ANYDBM provides the files that the interface uses to create and update databases. One of the limitations of the ANYDBM module is that both the key and the value are strings, meaning that it can only hold strings. The Pickle module converts almost all types of objects into a string form suitable for saving to a database, and converts a string into an object. Python encapsulates the above two modules into a module called shelve.
6. The pipeline (Os.popen () has now been abolished, but not completely abolished)
Any program that starts in the character interface can be started in Python via a pipeline. Its return value is about the same as an open file.
>>> cmd ='ls-l'>>> fp =os.popen (cmd)>>>Printfp.read () total dosage1156DRWXRWXR-X 3 yinheyi yinheyi 4096 Sep 17:34 2048-rwxrwxr-x 1 yinheyi yinheyi 8519 Oct 27 20:29a.out-rw-rw-r--1 yinheyi yinheyi Oct 27 20:29hello.c-rw-rw-r--1 yinheyi yinheyi Oct 31 14:26hello.py-rw-rw-r--1 yinheyi yinheyi Oct 31 22:10main.c-rw-rw-r--1 yinheyi yinheyi 218 OCT 31 22:11Makefile-rw-rw-r--1 yinheyi yinheyi 1 09:52Sb.txt-rw-rw-r--1 yinheyi yinheyi 214 Nov 1 10:17SUM.PYDRWXRWXR-X 4 yinheyi yinheyi 4096 Sep 20 17:18tinyhttpd-rw-rw-r--1 yinheyi yinheyi 423 Oct 31 20:13turtle.py-rw-rw-r--1 yinheyi yinheyi 1130523 Oct 31 16:43Words.txt>>>PrintFp.close ()
7. Write the module:
We can import any file that contains Pyhon code as a module. If we define a function inside, we can call it.
Note: For parts of the module that do not have the necessary code to be executed, this part of the code is usually written in the following mode:
if __name__ ' __main__ ' : Print ' Hello, world!. ' ...
__NAME__ is a built-in variable that is set when the program starts, the program executes as a script, and the value of __name__ is __main__, at which point the following code is executed. This part of the code is skipped when it is imported as a module.
Chapter 15th Class and Object
In this book, this section is really too simple to surface, and nothing can be taken as notes;
16th Chapter classes and functions
1. What is a pure function? A function that returns a value, does not modify any object passed in as an argument, and does not have any side effects such as displaying a value or getting user input.
2. What is a modifier? Sometimes it is useful to modify an incoming parameter object with a function. In this case, the modification is visible to the caller, and the function that works is called the modifier.
17th Chapter Classes and methods
1. For a function written inside a class, we call it a method.
When we invoke a method, it is possible to use the principal and method names directly, for example (Xiaoming is a principal, and H1 is a method name):
class Hello (object): def H1 (): Print ' Hello, world!. ' = Hello () xiaoming.h1 ()
In a method, a principal is assigned to the first parameter. Traditionally, a method's mutual parameter is often called self.
2. Init method of Class
The Init method is a special method that is called when an object is initialized. Its full name is __init__. Like what:
def __init__ (self, hour = 0, minute = 0, second = 0): = Hour = minute = Second
2. __str__ Methods for classes
__init__ is also a special method that is used to return the expression of an object's string. When we print the results of an object, Python calls the __str__ method. Definitions such as:
def __str__ (self): return ' %d%d%d ' % (Self.hour, Self.minute, Self.second)
3. Operator overloading
By defining other special methods, we can specify the behavior for the various operators of the user-defined type, such as: when we define a method named __add__, we can use the + operator on the time object. Other operators have a corresponding special name method.
4. Polymorphic problems
The book does not elaborate, waiting to refer to other learning materials.
18th Chapter Succession
Read "Thinking about Python like a computer scientist"-Notes (2/2)