When chatting with a young man a few days ago, he found a problem. He learned Python from scratch. After learning the basics of lists, dictionaries and functions, he began to learn advanced in the direction of crawlers. As a result, it took more than a month to learn nothing, but he couldn't do anything.
In fact, the main reason for this problem is that I haven't learned the basic knowledge of Python well. The basic knowledge of Python I mentioned refers not only to basic knowledge such as lists, but also advanced programming content such as object-oriented and process threads.
This may be that beginners don't know how to lay the foundation. Unfortunately, the selected learning resources don't talk about advanced programming, which makes it difficult to learn later. Haste makes waste, so I personally think we should learn the basic knowledge well first.
Today, I wrote a teaching article about Python advanced programming. After you have learned the basics of lists, learning the contents of this piece will make your advanced road easier and smoother!
Let's take a look at what Python advanced programming includes:
1, Object oriented programming
➿ (1) Object oriented thinking
Object oriented is an abstract programming thinking, which many programming languages have. Python is a pure object-oriented language. In short, it can be understood as focusing on how to solve problems rather than studying how to implement them at the bottom.
Object oriented and process oriented are two different ideas. The language of object-oriented programming includes Python and other languages, and the language of process oriented programming includes C and other languages. The programming thinking in writing code is different.
For example, to understand these two ideas, take washing clothes as an example.
Washing clothes by hand is usually done like this: find a basin - drain water - add washing powder - Soak clothes - Scrub - wring dry - Wash - wring dry - air.
This is a process oriented idea. Once you don't know what to do in the middle, you can't wash the clothes well. For example, you don't know how to scrub the clothes, which leads to unclean washing. For example, you don't wring it dry after scrubbing, which leads to water on the ground. If something goes wrong in one link, you can't achieve the desired washing effect.
Washing clothes in the washing machine is usually done as follows: turn on the washing machine - put the clothes in - add washing powder - press the start button - dry.
This is an object-oriented idea. You don't need to know how to wash the washing machine after putting the clothes in. You just need to know how to pour the washing powder after putting the clothes in and then press the button.
Similarly, in the programming language, the embodiment is different. When writing code in C language, you have to pay attention to your memory and other low-level things, but when writing code in Python, we rarely pay attention to the low-level things. The focus is on what methods to solve problems.
Before formally involving the code, there is a conceptual thing that needs to be understood. After understanding these basic concepts, we can better enter object-oriented programming.
➿ (2) Two important concepts of object-oriented: class and object
1. Concept of object and class
These two concepts may be easier to understand when they are discussed together. It can be simply understood that a class is a template for generating objects.
We mentioned the example of washing machine above. If the washing machine is an object, the drawing for manufacturing this kind of washing machine is a class; If the egg is the object, then the hen is the class; If dogs are objects, then dogs are classes.
In Python, everything is an object, variables are objects, functions are objects, strings, lists, tuples and so on are objects.
2. Object details
(1) Object composition: object = attribute + method
Objects are composed of attributes and methods. Attributes can be understood as what objects have and methods can be understood as what objects can do.
(2) What are the characteristics of an object's properties
- The properties of an object can be abstract properties
For example, if the washing machine is an object, the washing machine has color. Color is an attribute, but color is an abstract thing because it can be red, white, black and so on.
- The property of an object can be another object
For example, if the computer is an object, then the computer has a hard disk. The hard disk is an attribute of the computer, so the attribute of the object can be another object.
- Large objects consist of small objects
For example, the mouse is an object, the mouse wheel is an object, the MCU in the mouse is an object, etc. you will find that the large object of the mouse is composed of multiple small objects.
(3) Object method
There is a rule to understand first. Object methods can be called by themselves or by other objects. I will explain it in detail later.
We won't talk about the code here for the time being, because we need to talk about the knowledge of classes first, so as to better understand the classes and objects in the code, which will be discussed below.
3. Detailed explanation of class
As mentioned above, class is the template for generating objects, so class is the abstraction of objects, and object is the concretization of classes.
Since object = attribute + method, similarly, class = attribute + method.
(1) Create class
Method to create class:
class Class name: Attribute 1 Attribute 2 Method 1 Method 2 #A class can have multiple properties and methods, only properties or only methods
For example, create a student class and use it to create objects
#Create Student class class Student: name = "Xiao Ming" #name attribute of student class age = 18 #age attribute of student class def learn(self): #Learning method for students print("The student's learning method was called") #Create object of Student class s1 = Student() #Student() is a class object created. It's just easy to abbreviate. I passed it to s1 s1.learn() #Object method print(s1.age) #Properties of the execution object #In fact, the above two lines of code can be directly written as Student().learn(), which also uses classes to create objects and call methods of objects
Execution results:
In fact, when creating the object of Student class, the following three lines of code can also be written as follows. The effect is the same:
Student().learn() #Create an object of the class and use the method of the object print(Student().age) #Create an object of the class and execute the properties of the object
In retrospect, let's understand what attributes and methods are from the perspective of the combination of code and concepts.
Class attributes are what is in the class. For example, there are name and age in the class, so name and age are the attributes of the Student class.
A class method is what a class can do. For example, there is a learn(self) function in the class, which can execute "the student's learning method has been called", that is, what the class can do, so the learn(self) function is a class method.
(1) Detailed explanation of attributes
We introduced the properties of objects and classes earlier, so let's take a look at the differences and characteristics between them.
- Class properties
1. Class properties are defined inside the class, not outside any method 1. When creating a class, create it directly 2. Class name.New property name=Attribute value 2. Use class properties: 1. object.Class properties 2. class.Class properties 3. Characteristics of class attributes: 1. Class properties belong to class objects 2. Objects share class properties. If the class properties change, all objects will also change 4. Class attributes have and only have one copy, which affects the global situation
- Object properties
Object properties are defined inside the method 1. establish 1. object.Instance property name=Attribute value 2. Method internal creation: self.Attribute name = Attribute value 2. Use object properties object.Instance property name 3. Object properties are independent of each other 4. Naming conflict between object property and class property 1. Object property name takes precedence 2. Object property names mask class properties with the same name
(2) Class
- Class method declaration
def Function name( self,Parameter 1, parameter 2,....)
- self
1.self By default, no value is required 2.self Refers to the current instance (object) 3.There must be self Pass, but the name does not have to be called self,Can call aa 4.self It can be followed by multiple parameters, separated by ","
- Method call
1.object.Method name (auto pass object), for example s2.fun1() 2.Class name.Method name (class name) #"Class name ()" is equivalent to creating an object Note: method calls are similar to functions. Whoever calls them will be returned to whom
- Initialized methods (Methods of special classes)
def __init__(): 1.This method does not need to be called, as long as the object is created, it will be executed automatically 2.This method can only return None Value, cannot return other types of values 3.If you want to give init Multiple parameters can only be passed by class name (multiple parameters) 4.If there is no definition in the class init,Default to call parent class 5.If there are multiple definitions in the class init Method, will be the last one init Method coverage
For example: create an object, view the default init and pass multiple values; call the method of one class to confirm that the method is called and execute the passed values.
#Create class class Student: name = "Xiao Mo" age = "18" def __init__(self,aa,bb): print("init Called") print(aa) print(bb) def fun1(self): print("Function 1") print(self) def fun2(self): print("Function 2") Student.fun1(Student(100,200)) #Create an object and pass multiple values to init
The execution results are:
init Called 100 200 Function 1 <__main__.Student object at 0x000001DAD8168400> #Storage address of self value
➿ (3) Three characteristics of object oriented
Three characteristics: encapsulation, inheritance and polymorphism.
1. Packaging
1. Objects have clear boundaries, and properties and methods are protected within the boundaries. (Security)
2. The packaging strength is moderate.
3. Principle of packaging
(1)Hide content that does not need to be provided externally. (2)Hide properties and provide public methods to access them Private properties:__name="xxx"
2. Succession
Inheritance is the relationship between parent and child classes, such as dog and erha. Dog is the parent class and erha is the child class.
(1) Definition form (full form of class)
class Class name of child class (class name of parent class): attribute method
(2) Parent class: base class, superclass
object--Top class If the object has no written inheritance relationship, it inherits by default object
(3) Characteristics of inheritance
- Subclasses can continue with the properties and methods of the parent class
Example: use the object created by the subclass to call the properties and methods of the parent class
# Define parent class class A: name1 = "Property 1 of parent class" def aa(self): print("Method 1 of parent class") # Define subclasses class B(A): name2 = "Subclass property 1" def bb(self): print("Subclass method 1") n = B() print(n.name2) #Call the properties of the subclass n.bb() #Call methods of subclasses print(n.name1) #Call the properties of the parent class n.aa() #Call the method of the parent class
The execution results are:
Subclass property 1 Subclass method 1 Property 1 of parent class Method 1 of parent class
-
Extensibility. The parent class is extended, and the child class is also extended.
-
If the subclass has no constructor, the object will call the parent class.
-
If a subclass has its own constructor, it will not call the constructor of the parent class.
-
The constructor of a subclass can call the constructor of the parent class in the following two ways:
Class name of the parent class.__init__(self) #Pass self manually super().__init__() #No need to add self
- Multiple inheritance
A subclass can inherit more than one parent class.
# Define parent class A class A: name_a = "Parent class A Property 1 of" def aa(self): print("Parent class A Method 1") # Define parent class B class B: name_b = "Parent class B Property 1 of" def bb(self): print("Parent class B Method 1") #Define subclass C class C(A,B): #Inherit two parent classes pass #skip n = C() print(n.name_a) print(n.name_b) n.aa() n.bb()
The execution results are:
Parent class A Property 1 of Parent class B Property 1 of Parent class A Method 1 Parent class B Method 1
Note: multi inheritance has its advantages and disadvantages. The advantage is that it enhances the scalability. The disadvantage is that it is easy to confuse the logic and difficult to understand after the inheritance relationship is complex. At the same time, it will also occupy a lot of resources. For example, the famous diamond inheritance problem mainly involves mro and C3 algorithms. Those who do not understand can baidu.
(4) Method coverage
- If the method in the subclass has the same name as the method in the parent class, the parent class is overwritten.
#Define parent class class Animal: def eat(self): print("Animals can eat") #Define subclass dog class dog(Animal): def eat(self): print("Dogs can eat") d = dog() d.eat()
The execution results are:
Dogs can eat
-
After the subclass overrides the overridden method, it essentially does not replace the method of the parent class. The method of the parent class still exists and can be called to other subclasses.
-
Precondition of method override: the method name of the subclass must be exactly the same as that of the parent class
(5) Method overloading
There are multiple methods (functions) with the same method name. In Python, the method is overloaded through the default value.
3. Polymorphism and polymorphism
Polymorphism: a class of things has many forms. It is a way to use objects. Subclasses override the methods of the parent class and call the methods of the same parent class of different subclass objects, which can produce different effects.
For example, take heroic hero as an example, Hero is a parent class. There is a method called stroke in the parent class. There are two subclasses in Cheng's gold and descendants, and stroke is also used in two subclasses. The caller is another parent class, and has a way to select heroes. This method calls for stroke class method, and finally calls different subclasses. (Cheng Yaojin or descendants) get different skill descriptions.
#Define parent class Hero class Hero : def stroke(self): #Skill function print("Heroic skill") #Define subclass Cheng Yaojin class ChengYaoJin(Hero): def stroke(self): #Skill function print("One skill jump, two skill rotation, big move to return blood") #Define subclass descendants class HouYi(Hero): def stroke(self): #Skill function print("One skill is multiple, two skills are circle shooting, big moves, big birds are dizzy") #Define caller class Person: def chose_hero(self,hero): #Players choose Heroes hero.stroke() c = ChengYaoJin() #Cheng Yaojin x = HouYi() #offspring p = Person() #game player p.chose_hero(c) #Players choose Cheng Yaojin, and changing "c" to "x" will produce different results
The execution results are:
One skill jump, two skill rotation, big move to return blood
Polymorphism: send different information to different objects, and different objects will respond differently after receiving the information.
#Defining birds class Bird: def fly(self): print("Birds can fly") #Define aircraft class class Plane: def fly(self): print("The plane can fly") #Define rocket class class Rocket: def fly(self): print("Rockets can fly") #The function calls the fly method of the parent class def fun1(obj): obj.fly() fun1(Bird()) #Call the fly method of the bird fun1(Plane()) #Call the fly method of the aircraft class fun1(Rocket()) #Call the fly method of the rocket class
The execution results are:
Birds can fly The plane can fly Rockets can fly
Two, modules and packages
👳 (1) Module
1. Definitions
The module refers to the python program file (source file). The file name of the module is the module name plus. py, which contains Python object definitions and python statements. The module includes defined functions, classes, execution code, etc. generally, do not modify the module to avoid module failure.
2. Module import
Python allows module calls in the form of import. Modules in Python are also objects. All global variables defined in modules become module properties after import.
Syntax 1: import module name
If you want to import multiple modules, you can import multiple modules with only one import, separated by commas. However, in the PEP8 style of Python, this is not recommended, so if you want to import several modules, write several imports to import them one by one.
Example: import math module and call sqrt() square function to square 9
import math num = math.sqrt(9) print(num)
Output results:
3.0
Note that the default return type for arithmetic operations in Python is float, so it is 3.0.
Syntax 2: from module name, import function 1, function 2
Note that there are no parentheses after the functions here.
Example: import math module and call sqrt() square function to square 9
from math import sqrt num = sqrt(9) print(num)
Output results:
3.0
Syntax 3: from module name import
Generally, this syntax can import all functions of a module. When you want to use multiple functions of a module, you don't have to write them one by one. I won't give an example here. They are the same.
Note:
This method can not import all functions in the module in all cases, If there is in the module__all__=["Function 1","Function 2"]Such a statement, Then the imported module can only use function 1 and function 2, If there are functions such as function 3 in the module, but not in__all__=[list]Cannot be called if declared in.
Example: (this example can only be understood after reading the following user-defined module)
A new module named module1 is created. The module code has two functions:
def fun1() : #Implement a+b and output the results print("fun1") def fun2() : #Implement a+b and output the results print("fun2") __all__ = ["fun1"] #Declaration can only call fun1()
In another Python file, import all methods in the module in the way of syntax 3:
from module1 import * fun1() fun2()
The execution result will only print out fun1, and then an error message will be sent, indicating that fun2() is not defined and cannot be recognized. This is because
stay module1 of use__all__Declared callable methods only fun1().
3. Define alias
Some modules or function names in modules are long and inconvenient to use for many times. You can customize the name of modules or functions.
1. Module user defined alias: import module name as alias
Example: the custom time module is alias t and used.
import time as t t.sleep(3) #Three seconds delay print("hello world")
Output after 3 seconds of program execution:
hello world
2. Function user defined alias: from module name import function name as alias
Example: import the time module and customize the sleep() function to the name s
from time import sleep as s s(5) #Delay 5 seconds print("hello world")
Output after 5 seconds of program execution:
hello world
4. User defined module
Everyone can generate a custom module to call. A custom module is a Python file. The Python file created when we write code is equivalent to one module.
Note: the called module should be placed in the same directory as the current Python file as far as possible, otherwise the folder should be declared during import.
Example: customize a module and call it in another Python file.
Create a new Python file named module1. The code is as follows:
def fun1(a,b) : #Implement a+b and output the results print(a+b)
Create another Python file in the same directory and call module1.py:
import module1 module1.fun1(20,30)
Results of running the current Python file:
50
5. Module test
Each module is executed by default when importing, but there are many internal test codes inside the module. In order to avoid executing the test code inside the module when importing the module, a method is involved:
Many modules have internal test methods: if __name__ == "__main__": Code 1
This method can realize a function. If it is executed in the module, the code of code 1 will be executed. When other files import the module, the code of code 1 will not be executed, so the internal tests of the module are generally placed in code 1.
Why? The magic point is__ name__, The result of its execution in the current file is__ main__, When importing other files, the result is the module name. Therefore, using this point, you can judge whether the module is executed in the current file or imported by using the if statement.
give an example:
Create a new Python module called module1 as a module. The code is as follows:
print(__name__) #Print__ name__
Execution results:
__main__
Create a new Python file and import the module1.py module just built:
import module1
Execution results:
module1
When the current file is executed and imported, the result is different, so it becomes the internal test method of the module.
Note: in custom modules, it is not recommended to write while recycling, otherwise the while recycling in the module may not jump out during the import process, that is, the module has been imported and other codes cannot be executed.
👳 (2) Package
1. Definitions
Package is to put the associated modules in the same folder, and there are“__init__.py"This file, this folder, is called a package.
Package features:
1.Packages are also objects 2.Must have__init__.py file 3.__init__.py File is the construction method of package, which controls the import behavior of package. It is generally an empty package 4.There can be not only modules, but also sub packages in the package
2. Package creation
Create a New project file in pychart software. After creation, open pychart to create a project folder → click folder → right click to pop up options → New → Python Package → complete creation. The created New folder is the package, in which the init file is automatically created.
3. Import package
There are two general import methods.
Method 1: import package name. Module name. Target
Method 2: import package name. Sub package. Module name. Target
The targets mentioned here can be variables, functions and other objects, which will be discussed below.
4. Use of package
Use form 1: regular use 1
import Package name.Module name Package name.Module name.function
For example: create a package and use it in another. py file. It is required that the. py file is not included in the created package.
Step 1: open pychar to create a new project, create a package named demo, and create a new. py file named my_module in the package. The code is as follows:
a = 100 def fun1(): print("This is a bag")
Step 2: open another. py file and import the created package
import demo.my_module print(demo.my_module.a) demo.my_module.fun1()
Execution results:
100 This is a bag
Use form 2: regular use 2
from Package name import Module name Module name.function Module name.variable
For example, I use the demo package created above, directly modify the code in step 2, and import and use the package in this new way.
from demo import my_module print(my_module.a) my_module.fun1()
The execution result is the same:
100 This is a bag
Use form 3: alias and use
import Package name.Module name as alias alias.function alias.variable
In form 1 above, some people may find that it is inconvenient to use the imported module. If you need both the package name and the module name, can you simplify it? Of course, you can directly give a shortened alias to the module in the package, and then use the alias directly.
Following the example of using form 1 above, I will not change step 1 here. I will directly alias it in step 2.
import demo.my_module as n1 #Another alias is n1 print(n1.a) n1.fun1()
Execution results:
100 This is a bag
The result is the same, but if the code is long, it will be much easier to use aliases.
Of course, you can also use form 2 to import the package and give another alias. When using it, you can use the alias. I won't give an example of this method. You can try it yourself.
Usage 4: import a function
from Package name.Module name import Function 1 Function 1
Here, I also use the created package directly in another. py file.
from demo.my_module import fun1 fun1()
Execution results:
This is a bag
Usage 5: import all functions
During module import, we introduced the method of import * to import all the functions in the module. Here, we can also import all the functions of the modules in the import package.
For example, in the package called "demo", there is a module called "hhhh". The code of the module is as follows:
def fun1(): print("This is function 1") def fun2(): print("This is function 2")
Then we call it in another.py file.
from demo.hhhh import * fun1() fun2()
Execution results:
This is function 1 This is function 2
6. Use of all
About__ all__ The use of the module was introduced in the module. It is a list of functions that can be imported, but in the package__ all__ It controls the list of modules that can be imported, that is, it declares which modules can be imported.
Inside the bag__ all__ Yes__ init__ File, not in which module.
For example, there are multiple modules in a package. The modules declared by all can be imported, and those not declared can not be imported.
Step 1, there are hhhh and my in the package named demo_ Module is two modules, but it is declared by all in the int file. Only hhhh file can be used. The code of int file is as follows:
__all__ = [ "hhhh" # Hhhhmodules are not allowed to be imported ]
Step 2, import hhhh and my in the demo package in the new. py file_ Module two modules:
from demo import * #Import all modules in the package hhhh.fun1() #Call the fun1 function of hhhh module and output "this is function 1" my_module.fun1() #Call my_ The fun1 function of module outputs "this is a package"
Execution results:
Obviously, although all modules have been imported with *, hhhh modules declared by all that can be imported can be used, but they are not declared by all_ Module module cannot be used and cannot be recognized by the system.
👳 (3) Role of modules and packages
1. Improve code reusability. Easy to use code can be used by more than one person, and many people can reuse it.
2. Improve the readability of the code. If all the codes are put in one. py file, the code will be too long, which increases the difficulty of understanding and maintenance. Therefore, some commonly used codes can be encapsulated into packages and modules, and a literal name can be used directly when needed, which reduces the number of codes and improves readability.
3. Reduce code redundancy. For some methods encapsulated in the module, we can directly use the parameters. We don't need to write the methods again, which takes up memory and reduces the redundancy of the code.
👳 (4) Installation of third-party library
Although Python has many built-in modules and packages, referred to as built-in modules for short, it is not enough to only use built-in modules. After all, built-in modules are limited. We often use third-party libraries. At this time, it is necessary to learn how to install third-party libraries (packages).
Today, let's introduce three installation methods of third-party modules and packages.
1. Install via pip
You can download and install the third modules and packages through the package management command pip. The premise is that the Python you install is installed according to the way I mentioned before in the Python entry. All the options are checked so that you do not have to configure the environment variables. First, check whether your PIP can be used.
Method: WIN+R calls up the operation window → enter cmd → if the following pip information appears, pip can be used
If there is a red letter prompt "pip in cmd is not an internal or external command, nor is it a runnable program or batch file", then you can manually configure the environment variables. If you really can't, go back and follow the steps I said to install Python to reinstall it.
Returning to the problem of how pip installs third-party modules and packages, first of all, we need to know the name of the third-party modules and packages we want to install. For example, pilot, a third-party library, is a very powerful tool library for image processing in Python. The installation method is as follows:
1.WIN+R Call up the run window 2.input cmd 3.input pip install Pillow 4.Wait for the download and installation to complete
Sometimes there are many red letters indicating that the download fails. This is normal. The reasons may be:
(1) The. pip version is too low. Upgrade the pip version and enter python -m pip install -U pip in the black window
(2) The network is not good. Just download it several times
2. Installation via pychar
You do not need to open the software to install through pip, but you can also install through pychart. The method is as follows:
Click File → Settings → Project: Project name → Python interpreter → click + →
Enter the package name you want to install, select the one you like → click Install Package → wait for download and installation
3. Download and install the. whl file through channels
You can search and download the package you want through the official website and other channels. Official website: https://pypi.org/ , search the third-party library you want to download, such as the pilot library. Just search it directly:
Then select the file you want to download, such as the version of pilot 8.3.2, enter the download page and click Download files.
There are many versions and models after you go in. It is recommended to choose the version that conforms to you, otherwise it may not be installed. Here, pay attention to the number of digits of Python version, system and computer.
For example, my Python is installed with 3.9.6, so I download cp39. The computer is 64 bit and uses Windows system, so my version is pilot-8.3.2-cp39-cp39-win_ Amd64.whl this file.
After downloading, WIN+R opens the command line, enters CMD, and enters the file name under the pip install file path in the black window. For example, the path I saved after downloading is D: \ Google browser, so the code I entered in the black window is:
pip install D:\Google browser\Pillow-8.3.2-cp39-cp39-win_amd64.whl
Then press enter to install and wait for the installation to complete.
3, File processing
💼 (1) File definition and operation
Files in the computer usually refer to the information set stored in the computer with the computer hard disk as the carrier. The main forms are video, audio, pictures and documents, such as executive file. exe, document file. txt, web page file. html and so on.
💼 (2) Basic operation of files
In reality, we can roughly summarize the operation of files as "open → operation (reading, deleting, modifying, etc.) → save → close". This is still the case in Python. Before starting file operation in Python, we should learn several methods.
1.open(name,mode) -- open the file
This is the Python method to open a file. It is used to open a file and return a file object.
Name refers to the file name, which must be written in full. What is written in full? It is necessary to write clearly the storage path + file name + suffix.
Why write so full? Because even under the same storage path, there may be more than one file with the same file name. As long as the suffix is different, the computer is allowed to have files with the same name, so if you don't write it all, the computer doesn't know who you mean
Mode is the mode for opening files. The default mode is r, which is read-only. There are many ways of mode, such as reading, writing and so on, which we will talk about later.
2.write("content") ---- write
As the name suggests, it is to write content to the file object.
3.read() ----- read
Write content to the file. Numbers can be written or not written in parentheses. If not written, the default is to read all the content. Writing numbers means reading X characters. For example, read(6) reads 6 characters of the file object.
4.close() -- close the file
The method of closing a file. If you do not close the file after the file operation, the file will always be open and operated, which will occupy memory.
5. Cases
After understanding these four basic methods, let's start with a small case: create a new project, and then create a Python file named "file" to write code, and then write "hello world" to a. txt file called "file 1" in the way of writing. The code is as follows:
f = open("Document I.txt",'w') #Open the file by writing f.write("hello world") #Write content to file 1 f.close() #Close file
As mentioned earlier, the open() method returns a file object, so let's use f to receive it. There is no pre run interface:
After operation:
After running, a new file named "file. txt" is generated. After opening it, we can see the contents we entered. During write operation, if the file does not exist, it will be created by default.
Similarly, we can also read this file:
f = open("Document I.txt",'r') #Open the file by writing print(f.read()) #read file f.close() #Close file
Operation results:
hello world
This is the most basic file operation process.
It should be noted here that when you start writing open(name,mode), the mode already determines what you can do, that is, if you start writing code:
f = open("Document I.txt",'r') #Open file as read-only
If you want to write() later, you will report an error. Because the mode declares the r read-only mode, you do not have write permission. Please pay attention to this.
6. Other operation modes of mode
There are many operation modes in mode. Let's look at them in the form of tables:
pattern | describe |
---|---|
r | Open the file as read-only with the pointer at the beginning |
r+ | Read and write, file pointer at the beginning |
rb | In binary form, the read-only file pointer is at the beginning |
w | Write only, if the file does not exist, create a new one, if it exists, overwrite it, and the pointer is at the beginning |
w+ | Read / write. If the file does not exist, create a new one. If it exists, overwrite it. The pointer is at the beginning |
wb | Write only, in binary form |
a | Append mode, file pointer at the end |
a+ | Read / write. If it does not exist, it will be created. If it exists, it will be appended directly |
ab | Append in binary form |
The pointer here can be understood as the cursor. Where it is, your operation starts from where it is.
For example, create a new. txt file named "test", write aaa to it for the first time and bbb to it for the second time.
f = open("test.txt", 'a+') f.write("aaa") f.close() f = open("test.txt") # Default read only mode print(f.read()) # print contents f = open("test.txt", 'a+') f.write("bbb") f.close() f = open("test.txt") # Default read only mode print(f.read()) # Print content again
Operation results:
aaa aaabbb
💼 (3) Methods and properties of files
1. Object properties of file
There are three common ways to view the properties of a file object:
1. closed If the file object is closed, return True,Otherwise return False 2. mode Returns the access mode of the file object 3. name Returns the name of the file
Case: operate the file (optional), and check the file name, operation mode and whether it is closed.
f = open("test.txt", 'a+') f.write("aaa") f.close() print(f.closed) #Check to see if it is closed print(f.name) #View file name print(f.mode) #View operation mode
Operation results:
True test.txt a+
2. Object method of file
There are many file methods. We have talked about them earlier, such as read() and write(), but there are some common methods to master, such as the following:
1. close() Close file---very important 2. read([count]) Read the contents of the file count: Number of bytes 3. readlines() Read everything and package it into a list 4. readline() Read a row of data and add reading. The read data cannot be read again 5. seek(offset,[from]) Modify the position of the pointer: from from The position has moved offset Bytes from: 0 It means from the starting position, 1 means from the current position, and 2 means from the end oofset: Number of bytes to move 6. write() Write content to file
For example, write aaabbbccc to the test.txt file and output the contents of the file as a list.
f = open("test.txt", 'a+') f.write("aaabbbccc") f.close() f = open("test.txt") print(f.readlines()) f.close()
Operation results:
['aaabbbcccaaabbbccc']
💼 (4) os module
os module is a module used to access the operating system. It is often used in file operation. The module should be imported before use.
import os
1. Functions of documents
1.os.rename(Original file name, new file name) -File rename 2.os.remove(file name) -Delete file If you do not specify the path, look for it in the folder where the source code is located. If you cannot find it, an error will be reported. If you want to delete the file in the specified folder, you need a specific path for the file name, such as os.remove(r"D:\test_1\file name"),r Prevent escape of slashes
For example, the existing file test1.txt is modified to test20.txt.
import os os.rename("test1.txt","test20.txt")
Operation results:
2. Folder functions
1.os.mkdir(Folder name) -create folder 2.os.rmdir(Folder name) -remove folders 3.os.getced() -Get current directory 4.os.chdir(catalogue) -Switch directory 5.os.listdir(catalogue) -Get all the files or folders in the current folder and return a list os.listdir("aa") #Get all files or folders under aa folder and return a list
Example: create a new folder in the existing folder venv.
import os os.chdir(r"D:\file\venv") #Switch to the venv folder. r is to prevent escape os.mkdir("New folder") #Create a new folder under the venv folder print(os.getcwd()) #Output current directory location
Operation results:
4, Abnormal
⚡ (1) Definition of exception
An exception is an event that occurs during program execution and affects the normal execution of the program. Generally, an exception occurs when Python cannot handle the program normally.
For example, I only wrote a variable a in the code to run the program. pycharm cannot recognize this variable, so an error is reported, which is an exception.
⚡ (2) Exception handling
So we need to master the methods of handling exceptions. There are many methods of handling exceptions. Let's look at them one by one.
1.try-except
It can process possible error codes. After processing, the red font of error will be converted into a short and normal font. The usage is as follows:
try: Code with possible exceptions except Exception type as variable Processed code
For example, printing variable a directly will report an error.
After Tyr except processing:
try: print(a) except NameError as s: print(s)
Run again to see the effect:
The red error message becomes short and the color is normal. Does this exception seem much more comfortable?
Tyr exception here does not affect the operation of the code. If your code does not report an error, even if you write Tyr exception, it will only execute the try line of code. If there is no error in that line of code, the code in exception will not be executed.
For example, let's have a normal:
2.try-except-except
This method is similar to the previous try exception method, but an exception is added later to judge a variety of possible error reporting situations.
For example, there are two lines of code that may report errors and two different types of exceptions, but you don't want to make it red.
try: 1 / 0 print(a) except NameError as s: # The first way is to use as + variable print(s) except ZeroDivisionError: # The second method is to customize the output content print("Divisor cannot be 0") # Customize the contents of the output
Operation results:
Although the error is reported, it is not red. Here, pay attention to the two ways of writing except.
Try exception is written flexibly. We can also use tuples to include the exception types that may report errors, so as to avoid writing multiple lines of exception, for example:
3.try-except-else
If there is no exception, execute the code in else, for example:
4.try-except-finally
Whether the code has exceptions or not, the code in finally will be executed in the end. For example:
5. Top level Exception
In fact, the error type can not be added after Exception, because the system will default that the following error type is Exception, which is a top-level class and contains all error types.
⚡ (3) Custom exception
Have you found that when we do basic exception capture, we have to write a try exception every time where there may be errors. If there are multiple places, there may be errors? Do we need to write multiple try excepts? Or in theory, the code can run, but I want to set rules. For any behavior that does not conform to my rules, I will make it abnormal. For example, if the password length exceeds the length specified by me, I want the program to be abnormal.
Custom exceptions can be used to throw an exception (throw an exception), which is thrown by the keyword raise.
For example: simulate the scenario of a user entering a password. The password entered by the user cannot be less than 6 digits. Customize an exception to detect whether the password entered by the user meets the requirements. If it does not meet the requirements, an exception will be raised, prompting that the length of the currently entered password and the minimum password length cannot be less than 6 digits.
class MyError(Exception): # Exception caught class def __init__(self, length, min_len): # Length is the length of the password entered by the user, min_len is the specified minimum length self.length = length self.min_len = min_len # Sets the description of the exception thrown def __str__(self): return "The length you entered is%s,Not less than%s" % (self.length, self.min_len) def main(): try: con = input("Please input a password:") # Get the password entered by the user l = len(con) # Gets the length of the password entered by the user if l < 6: raise MyError(l, 6) # If the length is less than the set 6 digits, an exception is thrown except Exception as ss: # Prompt if there is an error print(ss) else: print("Your password has been entered") # Execute if there are no errors main()
Operation results:
From the above code, we have used the previous knowledge of classes and instance objects in the object-oriented section. If you forget, hurry to review. In addition, we also combine the previous try exception and our keyword raise to cause exception capture.
5, Regular expression
✈️ (1) re module
Before we talk about regular expressions, we first need to know where to use regular expressions. Regular expressions are used in the findall() method, and most string retrieval can be done through findall().
1. Import re module
Before using regular expressions, you need to import the re module.
import re
2. The syntax of findall():
After importing the re module, we can use the findall () method, so we must know how the syntax of findall () is specified.
findall(Regular expression, target string)
It is not difficult to see that findall () is composed of regular expression and target string. The target string is what you want to retrieve, so how to retrieve is operated through regular expression, which is our focus today.
After using findall (), the result returned is a list of regular strings
✈️ (2) Regular expression
1. String matching
(1) Ordinary character
Most letters and characters can match themselves.
import re a = "abc123+-*" b = re.findall('abc',a) print(b)
Output results:
['abc']
(2) Metacharacter
Metacharacters refer to. ^ $+ Special characters such as {} \ [], through which we can personalized retrieve the target string and return the results we want.
Here I'd like to introduce 10 commonly used metacharacters and their usage. Here I'll give you a simple summary for easy memory. Next, I'll explain the use of each metacharacter one by one.
** 1️⃣ []**
[] is mainly used in the following three ways:
- Commonly used to specify a character set.
s = "a123456b" rule = "a[0-9][1-6][1-6][1-6][1-6][1-6]b" #For the time being, this troublesome method is used first, and then it is easier. There is no need to knock so many [1-6] l = re.findall(rule,s) print(l)
The output result is:
['a123456b']
- Can represent a range.
For example, to select the abc element in the string "abcabcaccaac":
s = "abcabcaccaac" rule = "a[a,b,c]c" # rule = "a[a-z0-9][a-z0-9][a-z0-9][a-z0-9]c" l = re.findall(rule, s) print(l)
The output result is:
['abc', 'abc', 'acc', 'aac']
- Metacharacters in [] do not work. They only represent ordinary characters.
For example, to select "caa" from the string "caabcabcaabc":
print(re.findall("caa[a,^]", "caa^bcabcaabc"))
The output result is:
['caa^']
Note: when it is in the first position of [], it means that all but a are matched. For example, change the position of and a in []:
print(re.findall("caa[^,a]", "caa^bcabcaabc"))
Output:
['caa^', 'caab']
2️⃣^
^It is usually used to match the beginning of a line, for example:
print(re.findall("^abca", "abcabcabc"))
Output results:
['abca']
3️⃣ $
$is usually used to match the end of a line, for example:
print(re.findall("abc$", "accabcabc"))
Output results:
['abc']
4️⃣ \
Different characters can be added after the backslash to indicate different special meanings. There are three common types.
- \d: Matching any decimal number is equivalent to [0-9]
print(re.findall("c\d\d\da", "abc123abc"))
The output result is:
['c123a']
\Can be escaped to normal characters, for example:
print(re.findall("\^abc", "^abc^abc"))
Output results:
['^abc', '^abc']
5️⃣ s
Match any white space characters, for example:
print(re.findall("\s\s", "a c"))
Output results:
[' ', ' ']
6️⃣ \w
Match any alphanumeric and underscore, equivalent to [a-zA-Z0-9_], for example:
print(re.findall("\w\w\w", "abc12_"))
Output:
['abc', '12_']
7️⃣ {n}
{n} You can avoid repeated writing. For example, we wrote \ w three times when we used \ W, but here we need to use {n}. N represents the number of matches, for example:
print(re.findall("\w{2}", "abc12_"))
Output results:
['ab', 'c1', '2_']
8️⃣ *
*Indicates zero or more matches (as many matches as possible), for example:
print(re.findall("010-\d*", "010-123456789"))
Output:
['010-123456789']
9️⃣ +
+Indicates one or more matches, for example
print(re.findall("010-\d+", "010-123456789"))
Output:
['010-123456789']
🔟 .
. is a dot, which is not obvious here. It is used to manipulate any character except line feed, for example:
print(re.findall(".", "010\n?!"))
Output:
['0', '1', '0', '?', '!']
1️⃣ 1️⃣ ?
? Indicates one or zero matches
print(re.findall("010-\d?", "010-123456789"))
Output:
['010-1']
Note the greedy mode and the non greedy mode.
Greedy mode: match as many data as possible, which is represented by adding a metacharacter after \ D, such as \ d *:
print(re.findall("010-\d*", "010-123456789"))
Output:
['010-123456789']
Non greedy mode: try to match as few data as possible, as shown by adding?, after \ d?, For example \ d?
print(re.findall("010-\d*?", "010-123456789"))
Output is:
['010-']
1️⃣2️⃣{m,n}
m. n refers to the decimal number, indicating that it is repeated at least m times and at most n times, for example:
print(re.findall("010-\d{3,5}", "010-123456789"))
Output:
['010-12345']
add? Means to match as little as possible
print(re.findall("010-\d{3,5}?", "010-123456789"))
Output:
['010-123']
There are other flexible ways to write {m,n}, such as:
- {1,} is equivalent to the effect of + mentioned earlier
- {0, 1} is equivalent to the previously mentioned? Effect of
- {0,} is equivalent to the effect of * mentioned earlier
That's all for the common metacharacters and usage methods. Let's take a look at other knowledge of regularization.
✈️ (3) Regular use
1. Compile regular
In Python, the re module can compile regular expressions through the compile() method, e.g.:
s = "010-123456789" rule = "010-\d*" rule_compile = re.compile(rule) #Returns an object # print(rule_compile) s_compile = rule_compile.findall(s) print(s_compile) #What is the object returned by print compile()
Output results:
['010-123456789']
2. Usage of regular objects
Regular objects can be used not only through findall() described earlier, but also through other methods. The effect is different. Here is a brief summary:
(1)findall()
Find all matching strings and re turn a list
(2)search()
Scan the string to find the re matching position (only the first one found)
(3)match()
Determines whether re is at the beginning of the string (matches the beginning of the line)
Take the object returned after compiling the regular by compile() above as an example. We don't need findall() here, but use match() to see the result:
s = "010-123456789" rule = "010-\d*" rule_compile = re.compile(rule) # Returns an object # print(rule_compile) s_compile = rule_compile.match(s) print(s_compile) # What is the object returned by print compile()
Output:
<re.Match object; span=(0, 13), match='010-123456789'>
It can be seen that the result is a match object, the starting subscript position is 0 ~ 13, and the match is 010-123456789. Since the object is returned, let's talk about some operation methods of the match object.
3. Operation method of match object
Here, let's introduce the methods first, and then I'll give examples. The common usage methods of Match objects are as follows:
(1)group()
Returns the matching string
(2)start()
Returns the start of the match
(3)end()
Returns the location where the match ended
(4)span()
Returns the position of a tuple: (start, end)
Example: use span() to operate on the object returned by search():
s = "010-123456789" rule = "010-\d*" rule_compile = re.compile(rule) # Returns an object s_compile = rule_compile.match(s) print(s_compile.span()) #Process the returned object with span()
The result is:
(0, 13)
4. Functions of re module
In the re module, besides the findall() function described above, there are other functions to introduce:
(1)findall()
I won't say more about this according to all the strings returned by the regular expression. I introduced it earlier.
(2) Sub (regular, new string, original string)
The sub() function is used to replace strings, for example:
s = "abcabcacc" #Original string l = re.sub("abc","ddd",s) #String processed by sub() print(l)
Output:
ddddddacc #Replace abc with ddd
(3) Subn (regular, new string, original string)
subn() replaces a string and returns the number of times it is replaced
s = "abcabcacc" #Original string l = re.subn("abc","ddd",s) #String processed by sub() print(l)
Output:
('ddddddacc', 2)
(4)split()
split() splits a string, for example:
s = "abcabcacc" l = re.split("b",s) print(l)
Output results:
['a', 'ca', 'cacc']
6, Process thread
🔰 (1) Multitasking operating system
The operating system can perform multiple tasks. For example, in our Windows system, in addition to the tasks currently being performed and you can see, there are many tasks being performed in the background. You can call out the task manager with Ctrl+Alt+Del and have a look.
I often see the properties of several core processors in my computer configuration. For example, my computer is 12 core, that is, the computer can execute 12 tasks at most and run 12 processes at most at the same time.
But why can our computers run hundreds of tasks at the same time?
In fact, this is due to the task scheduling of the operating system. Most operating systems schedule in the form of preemptive time slices. The system can quickly switch between multiple tasks in A very small time. For example, the 8-core operating system can theoretically only execute 8 tasks at the same time in one second, but the system may switch between hundreds of tasks in one second. Task A is executed once, Task B is executed once C task execution... As A result, many tasks can be executed within 1 second, resulting in hundreds of tasks visible to the naked eye.
The term is "macro parallel, micro serial". In fact, the computer can only execute the number of tasks that do not exceed the number of configured cores in extreme time, and 8 cores can only execute 8 tasks.
1. What is a process?
Now that we have talked about tasks, a process is a task. A process is equivalent to a task. It is the smallest unit for the operating system to allocate resources. In python, multitasking can be accomplished by using process, which is a way to realize multitasking.
2. What is a thread?
Multiple subtasks of a process are called threads. Threads are the smallest execution unit of a process. A process can have many threads, and the tasks executed by each thread are different.
Python supports both multiprocessing and multithreading. Next, we will start to learn about Python processes and threads.
🔰 (2) multiprocessing in Python (package)
If you use multiple processes, your Python code is executed line by line from beginning to end, which is actually executing a process, which should be well understood.
To make more use of CPU resources, we can use multiprocessing. Here is a package commonly used in Python multiprocessing. It has many functions, such as subprocessing, communication, sharing, execution in different forms, etc. Let's learn about some commonly used.
1.Process - process class
Process is a process class in multiprocessing, through which multiple processes can be realized. Let's take a look at its usage first, and then we will have practical examples.
Process(target,name,args,kwargs)
- Target is the target. Where is the new process to be executed by the system? You have to give the system a goal.
- Name is the name of the process. You can set it or not. The default is Process-N, n is from 1,2,3... N, and the system takes the name from small to large by default.
- args and kwargs are parameters that can be passed to the target.
There are many methods in Process, and the most commonly used method is start() to start the Process.
Process name.start() #Start process
For example, the written code is as follows. I want to see the effect of opening and not opening the multi process call function.
import time #2 functions to be executed simultaneously def music() : for i in range(5): #5 times print("Listening to music...") time.sleep(0.2) #Delay 0.2s to make the effect comparison more obvious def movie(): for i in range(5): print("Watch the video...") time.sleep(0.2) #Delay 0.2s music() movie() print("Main process execution completed")
When multiple processes are not started, the execution effect is as follows:
It can be seen that this is a very normal operation. The program runs from top to bottom line by line. If the three loops in music() are not executed, they will not be executed in movie (). If the two functions are not executed, they will not execute the print in the last line ("main process execution is completed").
Let's look at adding multiple processes to the code of the above case:
import time import multiprocessing # 2 functions to be executed simultaneously def music(): for i in range(5): # 5 times print("Listening to music...") time.sleep(0.2) # Delay 0.2s to make the effect comparison more obvious def movie(): for i in range(5): print("Watch the video...") time.sleep(0.2) # Delay 0.2s if __name__ == "__main__": # Solve the recursion problem when calling package under Windows system # Create child process music_process = multiprocessing.Process(target=music) movie_process = multiprocessing.Process(target=movie) # Enable process music_process.start() movie_process.start() print("Main process execution completed")
I added an IF statement to the code to judge__ name__ Well, why? Because in Windows system, the multiprocessing package will be recursive, that is, it will be executed repeatedly between "import module and call module". If you don't believe it, you can remove the if statement and report an error if you put all the code outside. This is a phenomenon that will occur in Windows system. mac, linux and other systems don't need to add ifl for judgment.
About__ name__ = I talked about "main" during the initialization of modules and packages. If you don't understand it, you can go back and have a look.
Operation effect:
It can be seen that after the process is started, there are three processes running at the same time. One is the main process executing from top to bottom, and the following output is "main process execution completed". The other two sub processes execute music () and movie() processes. From their execution speed, they are running at the same time, Therefore, it is not necessary to wait for the code in one of the functions to execute three times before starting the second function.
For the same code, your execution effect may be different from mine, because the effect is randomly allocated according to the current situation of the system, but it does not affect you to see that its result is multithreading.
Finally, as we mentioned earlier, args and kwargs can pass parameters in Process. Args is the transfer of general parameters. Kwargs passes parameters in the form of dictionary. Let's take the above code as an example.
2. Get the number of the current process
We mentioned earlier that there are multiple processes performing tasks at the same time during code execution. How can we view the number of the current process to know which processes are running? Which are the main processes and which are the child processes? Three methods. Let's take a look at the methods first, and then use them together with examples.
(1) Get the number of the current process:
You need to use the getpid() method in an os module. The usage is as follows:
os.getpid()
(2) Gets the name of the current process
The multiprocessing package is still used here. There is a current in it_ The method of process() is used as follows:
multiprocessing.current_process()
(3) Gets the number of the current parent process (main process)
Which parent process does the child process belong to? This uses getppid() in the os module. The usage is as follows:
os.getppid()
Then we can see the methods. Let's get and print the name and number of the current process and the number of the parent process based on the example just now.
import time import multiprocessing import os # 2 functions to be executed simultaneously def music(): print("music Child process name:", multiprocessing.current_process()) print("music Subprocess No.:", os.getpid()) print("music Number of main process:", os.getppid()) for i in range(5): # 5 times print("Listening to music...") time.sleep(0.2) # Delay 0.2s to make the effect comparison more obvious def movie(a, b): print("movie Child process name:", multiprocessing.current_process()) print("movie Subprocess No.:", os.getpid()) print("movie Number of main process:", os.getppid()) for i in range(5): print("Watch the video...") time.sleep(0.2) # Delay 0.2s if __name__ == "__main__": # Solve the recursion problem when calling package under Windows system # Create child process music_process = multiprocessing.Process(target=music) movie_process = multiprocessing.Process(target=movie, kwargs={"a": 30, "b": 40}) # Enable process music_process.start() movie_process.start() print("Main process number:",os.getpid())
Operation results:
Yes, as long as we use the method of obtaining threads, the number and name can be printed.
🔰 (3) Multithreading module
Multiple processes can run several tasks at the same time. As we mentioned earlier, the minimum unit of a process is a thread, so a thread can also perform multiple tasks. If a process has only one task (main process), it can also be said to have only one thread. For example, when we do not use multi process to run code, we can say one main process or one main thread.
1. Multithreaded Thread class
A commonly used module for multithreading is threading. There is a class that teaches Thread, which is similar to the Process class we used in multithreading. Let's take a look at the usage first:
Thread(target=None,name=None,args=(),kwargs=None)
- Target: executable target
- Name: the name of the thread. The default is Thread-N
- args/kwargs: target parameters
Similarly, multithreading should also have a method to start, which is similar to the previous one:
start()
There is also a method to get the thread Name:
threading.current_thread()
Knowing these knowledge points, we begin to give examples: use similar examples to the above to use our multithreading.
import threading,time def music(name,loop): for i in range(loop): print("Listen to the music %s , The first%s second"%(name,i)) time.sleep(0.2) def movie(name,loop): for i in range(loop): print("watch movie%s , The first%s second"%(name,i)) time.sleep(0.2) if __name__ =="__main__": music_thread = threading.Thread(target=music,args=("The closest person",3)) movie_thread = threading.Thread(target=movie,args=("Tang Tan 2",3)) music_thread.start() movie_thread.start() print("Main thread execution completed")
Operation results:
The closest person to listening to music , 0 time See the movie Tang Tan 2 , 0 time Main thread execution completed The closest person to listening to music , The first time to see a movie Tang Tan 2 , 1st time See the movie Tang Tan 2 , The closest person to listening to music for the second time , 2nd time
It can be seen that our multithreading is actually similar to multithreading. It can also run multiple tasks. Here we also add the use of parameters.
2. Inherit Thread class
In addition to using the above method to implement multithreaded tasks, we can also implement multithreading by inheriting classes.
For example: print "cool" and "hair gone" through multithreading.
import threading,time #Multithreading creation class MyThread(threading.Thread): def __init__(self,name): #initialization super().__init__() #Call the initialization method of the parent class Thread self.name = name #name becomes an instance property def run(self): #Thread to do for i in range(5): print(self.name) time.sleep(0.2) #Instantiate child thread t1 = MyThread("be doomed") t2 = MyThread("The hair is gone") t1.start() t2.start()
The class MyThread is created by ourselves. It inherits from the parent class threading.Thread. At the same time, we need to write the initialization method of MyThread and make preparations every time it is called. We have also talked about super (). int (). We have talked about it in the previous object-oriented article. If you don't understand it, you can take a look at the content of the object-oriented article.
Operation results:
be doomed The hair is gone be doomed The hair is gone Cool hair is gone Cool hair is gone be doomed The hair is gone
There are random effects. Your effect may be different from mine. When running multithreaded code on each computer, whichever thread can grab the time slice will execute first.
Multithreading can be implemented through class Thread inheritance.
7, Container / iteration object / generator
🚩 (1) Container
In Python, a container is a data structure that organizes multiple elements together, and the elements in the container can be obtained iteratively one by one. To put it bluntly, its function is just like its name: it is used to store things (data).
Container actually does not exist. It is not a data type, but an artificial concept. It is just a concept word created to facilitate learning. It can use the membership operator (in or not in) to judge whether the object is in the container.
Of course, I didn't create it. I don't have such a big skill. It was created by the official. Well, you don't have to worry. I'm teaching you some strange nouns that others can't understand... That's what it's called in python.
Common container types are list, tuple, str, dict, and set.
Since the data in the container can be obtained iteratively, we have to learn a new concept: iteratable object.
🚩 (2) Iteratable object
What is an iteratable object?
In python, an iteratable object does not refer to a specific data type, it refers to a container object that stores elements.
In other words, if there is no data stored in the container, it is not an iterative object. Not all containers are iterative objects. The container contains but is not limited to iterative objects.
Note two points:
1.Many containers are iteratable objects (containers contain iteratable objects). 2.An iteratable object cannot iterate independently. The iteration is through for All iteratable objects can be used directly for Loop access.
You should not be unfamiliar with the for loop? Have you ever thought about how to implement the inside of the for loop? For example, in the example of the for loop, why can each element in the list be output? How is it implemented internally?
In fact, the for loop does two things:
1.use __iter__() Return an iterator. The iterator will be described below. We know that there is such a thing first. 2.use __next__() Gets each element in the iterator.
So we don't need a for loop to output every element in the list,
l = [1,2,3,4] # for i in l: # print(i) ite =l.__iter__() #What did ietr() do print(ite) #Print print(ite.__next__()) #Step 1 when the for loop does the second thing print(ite.__next__()) #Step 2 when the for loop does the second thing print(ite.__next__()) #Step 3 when the for loop does the second thing print(ite.__next__()) #Step 4 when the for loop does the second thing
Output results:
It can be seen that if we remove which line of code to print ite, the execution effect is the same as that of each element in the for loop output list. In the for loop, the range is limited to 4 times, and in fact, it is executed once__ iter__ () and 4 times__ next__ (), that is, the essence of the for loop accessing the iterative object is realized in this way.
Moreover, the two things that the for loop essentially does are indispensable, that is, if it doesn't__ iter__ () returned the iterator first__ next()__ We can't get the elements, which just shows the second point of the two points we should pay attention to earlier: an iteratable object can't iterate independently.
There are two built-in functions with the same principle and the same essence. Generally, it is more convenient to use built-in functions without writing so many underscores:
Built in function iter() The essence of is __inter__() ,Also returns an iterator. Built in function next() The essence of is __next__(),Also get the element after having the iterator.
As like as two peas, we can see that the result is the same. Now that we have talked about iterators, let's see what iterators are.
🚩 (3) Iterator
We can probably see from the for loop example above,
As long as it is realized__iter__()and__next__()An iterator is an iteratable object. In short, iterators have__iter__()Generated by__next__()Call.
In that case, when we learned the basics of Python, we said that range() is an iterative object, so it can also be passed__ iter__ () generate an iterator.
🚩 (4) Sequence
I mentioned sequence in the special article [assignment statement]. Here again, sequence is also an abstract concept. It contains lists, tuples and strings. It does not exist in itself and is also a concept word created for learning.
Iteratable objects contain sequences. Since sequences contain lists, tuples and strings, which are also involved in our previous examples, sequences can be used by iter () and next ().
Sequences can be divided into finite sequences and infinite sequences. A finite sequence has a range. For example, range(10) has limited the range. On the contrary, an infinite sequence is a sequence without a limited range.
Let's generate an infinite sequence. Here we need to use a new module itertools. Itertools is an iterative function set for efficient loops. Under it, there is a method count(), which can generate iterators without scope, which can be understood as infinite iterators.
Through this example, we can see that as long as it is executed once, next() will get the contents in the iterator once and get them one by one. I only wrote four next() here. If you write more times, you will output more times.
A mechanism like next() that is called whenever it is needed is called lazy loading mechanism, which is also called lazy loading;
On the contrary, there is hungry man loading. For example, the for loop will get all the objects in the iterator as soon as it is executed.
🚩 (5) List derivation
List derivation is related to generator. Before talking about generator, you need to know what is list derivation. List derivation is a method of generating list. The syntax is as follows:
l = [i for i in Iteratable object]
i represents the object to be placed in the list, and the for loop is a formula.
For example, let's use list derivation to generate a list:
l = [i for i in range(5)] print(l)
Operation results:
[0, 1, 2, 3, 4]
Using list derivation can easily generate the list we want.
At the same time, it also has many flexible uses, such as adding conditional judgment after it
l = [i for i in range(5) if 4<5] print(l)
Operation results:
[0, 1, 2, 3, 4]
if the following conditions are judged to be true, the list can be generated normally. if false, the list derivation is invalid, and l will be an empty list at this time.
There are other flexible uses, such as operating the previous i, such as doubling the values of i:
We can also output the iteration object by changing it into a string, but * represents the repetition operator in the string, so the effect is as follows:
In addition, in the previous i*2, we can also use functions to operate, such as:
In a word, list derivation is a flexible way to quickly and customize the generation of lists.
Then someone may draw inferences from one instance. List derivations are operated with []. Can you operate with ()? Will it generate a tuple? Let's take a look:
After [] is replaced by (), a generator generrator is returned. Let's talk about the generator again:
🚩 (6) Generator
Generator is a real object in Python, which is different from the concept of container. It can be called directly through next().
1. The first method to create a generator: generator expression
The first creation method is similar to the list derivation, that is, [] is replaced by ():
l = (i for i in Iteratable object)
For example, let's generate a generator to see if we can call it directly with next():
l = (i for i in "abcd") print(next(l))
Operation results:
a
As you can see, the generator can be called directly. Then, since the generator can be called by next(), the generator is a special iterator and an iteratable object.
2. The second creation method of generator: yield
In addition to using the above method to create a generator, you can also use yield to create it, as follows:
yield keyword
For example, we use a function that contains yield to create a generator:
def fun(): a = 10 while 1: a += 1 yield a b = fun() print(b)
Operation results:
<generator object fun at 0x000001F2AD95E900>
The result is that a generator is generated, and the function fun() is no longer a function. It is a generator. As long as yield appears in the function, the function becomes a generator.
Why does the while loop not always execute? Don't worry, let's output:
def fun(): a = 10 while 1: a += 1 yield a b = fun() print(next(b)) print(next(b)) print(next(b))
Operation results:
11 12 13
I called it three times, so it ran three times. Although the while loop exists, it doesn't work because of the lazy loading we mentioned earlier.
When you need it and when you call next(), it is lazy loading. Unlike hungry loading, all objects are generated in advance. If you replace it with a for loop, for example:
def fun(): a = 10 while 1: a += 1 print(a) b = fun()
After running, the program will enter an endless loop and add 1 to A. you can try the effect. This is that hungry loading generates iterators in advance and calls all iterator objects. Hungry loading takes up a magnifying glass of resources.
Finally, a diagram is used to summarize their relationship:
8, Modifier
🏁 (1) Concept and function of modifier
1. What is a modifier?
Decorator is also called decorator. It is also a function. It adds some additional functions to the original function or method.
2. Function of modifier
Generally speaking, the function of decorators is to add additional functions to existing objects.
For example, this function is a registered function, but sometimes when this user performs this operation, he is a registered user. I have written this function and don't want to move it. Then we can add a login function to this function through the modifier.
It is often used in scenarios with various requirements, such as log insertion, performance testing, transaction processing, caching, permission verification and so on. Decorator is an excellent design to solve this kind of problem. With decorator, we can extract a large number of similar code that has nothing to do with the function itself and continue to reuse it.
Let's learn the specific operation of modifier slowly.
🏁 (2) Use of modifiers
1. Instructions
Before using the decorator, we have to remember several instructions about the decorator:
(1) The keyword of modifier is @. As long as it appears in Python code, you can think of it as modifier.
(2) Modifiers modify functions or methods and cannot modify a class.
(3) Modifiers must appear on the first line of the modified function or method. Modifiers cannot be defined on the same line of the function.
example:
Although the modifier itself is a function, its appearance is regulated. The modifier above does not appear in the first line of the modified function or method, so it can't even execute the line of print("Uncle long").
(4) The modifier itself is a function, which passes the modified function as a parameter to the modifier, executes the functions in the modifier, returns the passed in function object, and calls the returned function.
These points are very important. Let's deepen our understanding through various ways of using modifiers.
2. Basic usage
If the modified function is not called, execute the function after @ and pass the modified function as a parameter, then the return value of the modifier function can be any value.
example:
def dec(a): #Modifier function print(a) #Look what the parameter passed print("helloworld") return None @dec #Using modifiers def funA(): #Modified function pass
Operation results:
<function funA at 0x0000018D849BB670> helloworld
First of all, we can see that this is a very simple example of the modifier. Use @ Dec to call the dec () modifier function, and the modified function funA () does nothing.
Secondly, let's look again. The modified function funA() does not call anything, but it is passed to the modifier function dec () as a parameter, so dec () needs a formal parameter to accept the passed value. I use a as the formal parameter. If you remove a, the system will report an error, because the value returned by the modified function funA() has nothing to accept. You can try.
Finally, the modified function funA() has not been called, do you notice? So the modifier function dec() returns anything. The above returns None. You can return a "sick" or anything. Next, let's look at the call of the modified function funA().
If the modified function is called, the modifier is directly executed and the modified function is delivered to the modifier as a parameter, but the return value of the modifier must be the current parameter.
example:
def dec(a): print(a) print("helloworld") return "be ill" @dec #Using decorator functions def funA(): pass funA() #Call the modified function
Operation results:
<function funA at 0x000001F78A48B670> helloworld Traceback (most recent call last): File "E:\Python-learn\Iteratable object\Modifier\Modifier_test.py", line 11, in <module> funA() TypeError: 'str' object is not callable
There is an error saying that the string str cannot be iterated. Why? In fact, the reason is that when a modifier exists and is used, the modified function is called again. At this time, the return value cannot be any value. At this time, the modifier function can only return the passed value. You can try to "sick" return If you change it to return a, the output will be normal. Or if you remove the line of funA() and do not call the Theseus function, the output will be normal.
def dec(a): print(a) print("helloworld") return a @dec def funA(): pass funA()
Operation results:
<function funA at 0x0000020F5664B670> helloworld
business as usual.
Let's take a look at the execution logic of the modifier:
def dec(a): print(a) print("Modifier function") return a @dec def funA(): print("Modified function") funA() print("Uncle long")
Operation results:
<function funA at 0x000001D90E75B670> Modifier function Modified function Uncle long
From this operation result, we can see that the operation logic of the modifier is: the modified function funA() is called, but it is not directly executed. Instead, it directly executes the modifier dec, and sends the modified function funA() as a parameter to the modifier dec, executes the code in the modified function dec(), and returns the code in the modified function funA() after passing the value a, Finally, the modified function funA() executes the rest of the code.
Let me use a more sketchy diagram to show:
3. Other usage: function nesting
We talked about the basic usage of modifiers earlier. There are many ways to use modifiers, and you can also use function nesting.
Use a simple example to demonstrate:
def A(x): #Modifier function def B(): #Functions nested in decorator functions print("B") B() return x @A #Using modifiers def C(): #Modified function print("C") C() #Call the modified function
Operation results:
B C
From the running results, the modified function can also be nested.
4. Other usage: closure
Closures are also a kind of functions, but they are special. I won't repeat the knowledge about closures here. We have talked about closures in [Python basics]. If you forget, you can go to see or Baidu. Let's see how closures in modifiers are used.
def A(x): def B(): print("B") return x() # C indifference call can only be made at one level. Here, it cannot be called through x return B @A def C(): print("C") C()
Operation results:
B C
It can be seen that closures can also be used normally in decorator functions.
5. Other usage: the modified function has the form of parameters
If the modified function has parameters passed, the parameters can only be passed to the embedded function in the modifier function.
def A(x): #Modifier function print(x) def B(aa, bbb): # The embedded function receives the parameters passed by the modified function print("B") print(aa, bbb) return x(aa, bbb) # C return B @A def C(n, nn): #Modified function print("C") C("10", "20")
Operation results:
<function C at 0x00000206BED6B670> B 10 20 C
It can be seen that although the modified function C () passes parameters to the modifier function A (), the object C () is passed by default, and the modified function C () is accepted by the modifier function A (). The parameters are passed to function B () in the modifier function A ().
Even if you add two formal parameters to A (), it will not accept it and will only report an error. As we said in the instructions for the modifier earlier, "the modified function is passed to the modifier as A parameter", so the modifier function only accepts the object of the modified function. If other parameters are to be passed, Then there must be other functions in the modifier function to accept the passed parameters.
6. Other usage methods: modifiers with parameters, functions without parameters, and embedded functions are used to collect parameters
If the modifier has parameters but the modified function has no parameters, you can only use embedded functions to receive parameters.
def fun(a=20): print(a) def c(bb): print(bb) return bb # It can be called indiscriminately, because the funB received in the second layer is equivalent to the first layer return c @fun(30) def funB(): print("xixixi") funB()
Operation results:
30 <function funB at 0x0000025DAE4DD0D0> xixixi
🏁 (3) Python's built-in decorator
What we mentioned earlier are all our custom modifiers. There are built-in modifiers in Python. Let's learn about three common Python built-in modifiers: staticmethod, classmethod and property.
Their function is to change the methods in the class into static methods, including class properties and class methods. Let's have a brief look at the specific use.
1.property
Property can change a method into a property. The modified method name must be the same as the method name under property. Property can only be used for private properties.
Let's make a comparison. Without using property, we create a class and use the methods in the class. It is as follows:
class A: def __init__(self): self.age = 20 #Instance properties def getattr(self): #Print instance properties print(self.age) def setattr(self,newage): #Assign values to instance properties self.age = newage a = A() a.setattr(30) a.getattr()
Operation results:
30
There is no problem. The operation results are normal. Let's try the difference with the built-in modifier property.
Before using the built-in modifier property, we have to add one point: private attributes. The attributes created inside the function are private attributes. They cannot be used outside the function without special treatment. The private attributes are preceded by two underscores, such as those in the class__ name means private attribute. Let's take a simple example:
It can be seen that private attributes can be accessed inside the class, but not outside the class (they can be accessed after some processing. They are not introduced here. You can check them online).
Back to our built-in modifier property, in the previous example where property is not used, we modify it and add the built-in modifier property.
class A: def __init__(self): self.__age = 20 @property def age(self): #Modified method return self.__age @age.getter def age(self): #Modified method return self.__age @age.setter def age(self, newage): #Modified method self.__age = newage a = A() a.age = 200 print(a.age)
Operation results:
200
From this example, we can see that the built-in modifier property can be used for private properties and can change methods into properties. For example, a.age is calling properties instead of methods. Although the method age() appears many times in the class, it does not report an error because of method coverage, but also because of the existence of property, Property specifies that the modified method name must be the same as the method name under property.
2.staticmethod – static method
The built-in modifier staticmethod is a static method. Its function is to extract the modified method from the class and become an independent function. This function cannot access the properties of the class.
Let's write a simple class first, and make a comparison between using and not using static method. If not used
class B: def __init__(self, name): self.name = name def eat(self): # Print passed values print(self.name) b = B("Uncle long") b.eat()
Operation results:
Uncle long
There is no problem with this. Let's take a look at the use of static method and add it directly:
An error is reported. The reason is that after adding @ staticmethod, the eat() method becomes an ordinary function. Although its position is in the class, it is actually equivalent to an ordinary function, not a class method, so you have to pass a value to it, otherwise the formal parameter self has no value to pass, so an error is reported.
The correct wording should be:
Let's summarize the static method:
1. This function is an ordinary function. Only this class can be used 2. Static methods can set parameters or do not need parameters(self) 3. The function cannot access properties of a class
3.classmethod
The difference between the method modified by classmethod and the instance method is that the first parameter received is not self, but cls (the specific type of the current class). The modified method cannot access the instance property, but can access the class property.
class B: age = 10 def __init__(self, name): self.name = name def sleep(self): #Print print(self) @classmethod def eat(cls): # Modified function print(cls) #See if you are passing a class or a value print(cls.age) #Access the properties of the class print(self.name) #Accessing the properties of an instance object b = B("Uncle long") b.sleep() b.eat()
Operation results:
<__main__.B object at 0x0000024FD7B3CFA0> <class '__main__.B'> 10 Traceback (most recent call last): File "D:\pythonProject1\Topic 2.py", line 21, in <module> b.eat() File "D:\pythonProject1\Topic 2.py", line 14, in eat print(self.name) NameError: name 'self' is not defined
It can be seen from the results that from the objects printed by sleep() and eat(), the object passed by sleep() is the created instance object b, while the function eat() modified by modifier passes the class; Judging from the attributes and instance attributes of the eat() access class, there is no problem accessing the attributes of the class, but an error is reported when accessing the attributes of the instance object.
Therefore, the difference between the method modified by classmethod and the instance method is that the first parameter received is not self, but cls (the specific type of the current class). The modified method cannot access the instance attribute, but can access the class attribute.
~
Operation results:
<__main__.B object at 0x0000024FD7B3CFA0> <class '__main__.B'> 10 Traceback (most recent call last): File "D:\pythonProject1\Topic 2.py", line 21, in <module> b.eat() File "D:\pythonProject1\Topic 2.py", line 14, in eat print(self.name) NameError: name 'self' is not defined
It can be seen from the results that from the objects printed by sleep() and eat(), the object passed by sleep() is the created instance object b, while the function eat() modified by modifier passes the class; Judging from the attributes and instance attributes of the eat() access class, there is no problem accessing the attributes of the class, but an error is reported when accessing the attributes of the instance object.
Therefore, the difference between the method modified by classmethod and the instance method is that the first parameter received is not self, but cls (the specific type of the current class). The modified method cannot access the instance attribute, but can access the class attribute.
That's all for today's sharing. Welcome to leave a message in the comment area!