objects can store data using ordinary variables that belong to an object, and variables that belong to an object or class are called fields; objects can also use functions that belong to the class, such as methods of classes, and fields and methods can be called properties of classes.
There are two types of fields--those that are instances or belong to the class itself, which are called instance variables and class variables, respectively.
Class is created by using the keyword class, and the fields and methods of the class are listed in a indented block.
The method of a class must have an extra first argument, but it is not assigned a value at the time of the call, this particular variable refers to the object itself, which, by convention, has the name self, similar to this in C #.
The code is as follows |
Copy Code |
Class Animal: Pass #empty Block |
The __init__ method calls the method when an object of the class is created, which is equivalent to a constructor in C + +.
The __del__ method calls this method when the object of the class is destroyed, which is equivalent to the destructor in C + +. The __del__ method is called when you delete an object using Del.
All class members (including data members) in Python are public, with one exception, or private if the data member used is prefixed with a double underline.
The code is as follows |
Copy Code |
Class Person: Count = 0 def __init__ (self, Name, age): Person.count + 1 Self.name = Name Self.__age = Age p = person ("Peter", 25) P1 = Person ("John", 20) Print Person.count #2 Print P.name #peter Print p.__age #AttributeError: person instance has no attribute ' __age ' |
Inheritance: In order to use inheritance, the name of the base class is followed by the class name as a tuple, and Python supports multiple inheritance. Here is an example of inheritance:
The code is as follows |
Copy Code |
1 class Schoolmember: 2 ' represent any school. ' 3 def __init__ (self, Name, age): 4 Self.name = Name 5 Self.age = Age 6 print "Initializing a school member." 7 8 def Tell (self): 9 "' Tell Me Details '" Ten print "Name:%s, Age:%s,"% (Self.name, self.age), 11 Class Teacher (Schoolmember): "' represent a teacher." def __init__ (self, name, age, salary): Schoolmember.__init__ (self, name, age) Self.salary = Salary Print "Initializing a teacher" 18 def tell (self): Schoolmember.tell (self) Print "Salary:%d"% self.salary 22 Class Student (Schoolmember): "' represent a student." def __init__ (self, name, age, marks): Schoolmember.__init__ (self, name, age) Self.marks = Marks Print "Initializing a student" 29 def tell (self): Schoolmember.tell (self) Print "Marks:%d"% self.marks 33 Print schoolmember.__doc__ Print teacher.__doc__ Print student.__doc__ 37 t = Teacher ("Mr Li", 30, 9000) s = Student ("Peter", 25, 90) 40 Members = [T, S] 42 For M in members: M.tell () |
The program output is as follows:
The code is as follows |
Copy Code |
Represent any school. Represent a teacher. represent a student. Initializing a school member. Initializing a teacher Initializing a school member. Initializing a student NAME:MR Li, age:30, salary:9000. Name:peter, age:25, marks:90 |
Viii. Input/Output
The interaction between the program and the user requires input/output, mainly including consoles and files, and the Str class can be used for the console using Raw_input and print. Raw_input (XXX) Enter XXX and then read the user's input and return.
1. File input/Output
You can use file classes to open a file, using the read, ReadLine, and write files to properly read and write to the document. The ability to read and write files depends on the mode used to open the file, common patterns
Read mode ("R"), Write Mode ("W"), Append Mode ("a"), and the Close method must be called after the file operation to shut down the file.
The code is as follows |
Copy Code |
1 test = ' ' 2 This are a program about file I/O. 3 4 Author:peter Zhange 5 DATE:2011/12/25 6 "" 7 8 F = File ("Test.txt", "W") # Open for writing, the file would be created if the file doesn ' t exist 9 F.write (test) # Write text to file F.close () # Close the file 11 f = File ("test.txt") # If no specified, the default mode is readonly. 13 The True: line = F.readline () If Len (line) = = 0: # Zero length indicates the EOF of the file Break Print line, 19 F.close () |
2. Memory
Python provides a standard module that becomes pickle, using it to store any Python object in a file, which can then be completely removed, known as a persistent storage object, and another module to be cpickle, which functions exactly like Pickle, only But it is written in C, faster than pickle (about 1000 times times faster).
The code is as follows |
Copy Code |
Import Cpickle datafile = "Data.data" NameList = ["Peter", "John", "King"] f = File (DataFile, "w") Cpickle.dump (NameList, F) F.close () Del NameList f = File (datafile) Storednamelist = Cpickle.load (f) Print Storednamelist #[' Peter ', ' John ', ' King ' |