Cat brother teach you to write modules reptile 026--

What is a module?

Class variables and functions are encapsulated

Module is the highest level of program organization unit. What this means is that what can be encapsulated module

In the module, we can not only direct variable storage, but also storage function, can store class

Definition of variables needed assignment, the wrapper function need def statement wrapper classes need class statement,

However, the statement does not require any encapsulation module

Each individual Python code file (extension is **. Py ** file) is a separate module

For the purpose of the package module is to store program codes and data for use together again.

If encapsulated into classes and functions, mainly to facilitate their calls,

However, the package module, we can not only own use, as files can be easily shared with other people to use

Using the Module There are two main ways, one is to establish their own modules and use the other one is using someone else shared module

Own modules

Py in the main program file, use the import statement to import other file py

Use the import statement to import a module, the main purpose of executing the statement does not run in the module,

But to have been packaged with a module variable, function, class

Analyze the output code ...

# test.py
a = '我是模块中的变量a'
def hi():
    a = '我是函数里的变量a'
    print('函数“hi”已经运行!')
class Go1(): 
    a = '我是类1中的变量a'
    @classmethod
    def do1(cls):
        print('函数“do1”已经运行!')
class Go2:
    a = '我是类2中的变量a'
    def do2(self):
        print('函数“do2”已经运行!')
print(a) 
hi() 
print(Go1.a) 
Go1.do1() 
A = Go2() 
print(A.a) 
A.do2() 
复制代码

The introduction of a file in another file ... (the analysis result outputted ...)

# index.py
import test  
print(test.a)  
test.hi()  
print(test.Go1.a)   
test.Go1.do1()  
A = test.Go2()  
print(A.a)  
A.do2()  
复制代码

Once a mountain ...

sentence = '从前有座山,'
def mountain():
    print('山里有座庙,')
class Temple:
    sentence = '庙里有个老和尚,'
    @classmethod
    def reading(cls):
        print('在讲故事,')
class Story:
    sentence = '一个长长的故事。'
    def reading(self):
        print('讲的什么故事呢?')
for i in range(10):
    print(sentence)
    mountain()
    print(Temple.sentence)
    Temple.reading()
    A = Story()
    print(A.sentence)
    A.reading()
    print()
复制代码

The code is split into two modules, executed statements into main.py file, packaged variables, functions and classes into story.py file

# main.py
import story
for i in range(10):
    print(story.sentence)
    story.mountain()
    print(story.Temple.sentence)
    story.Temple.reading()
    A = story.Story()
    print(A.sentence)
    A.reading()
    print()
复制代码
# story.py
sentence = '从前有座山,'
def mountain():
    print('山里有座庙,')
class Temple:
    sentence = '庙里有个老和尚,'
    @classmethod
    def reading(cls):
        print('在讲故事,')
class Story:
    sentence = '一个长长的故事。'
    def reading(self):
        print('讲的什么故事呢?')
复制代码

To the module from Alias ​​...

That import story is too long, you can use the import story as s statement, meant for the "story" to take individual named "s"

# main.py
import story as s
for i in range(10):
    print(s.sentence)
    s.mountain()
    print(s.Temple.sentence)
    s.Temple.reading()
    A = s.Story()
    print(A.sentence)
    A.reading()
    print()
复制代码

When the need to import the plurality of modules may be separated by commas.

For example import a, b, c can be introduced "a.py, b.py, c.py" three documents simultaneously.

If I want a variable with a module, or a certain function?

from ... import ... statement

** from ... import ... ** statement allows you to import a specified part from the module to the current module

# 【文件:test.py】
def hi():
    print('函数“hi”已经运行!')
# 【文件:main.py】
from test import hi  # 从模块test中导入函数“hi”
hi()  # 使用函数“hi”时无需加上“模块.”前缀
复制代码

Need to import content from a plurality of the specified module, may be separated by commas, written from import a, b, c xx module form

from test import a,hi,Go1,Go2
print(a)  # 打印变量“a”
hi()  # 调用函数“hi”
print(Go1.a)  # 打印类属性“a”
Go1.do1()  # 调用类方法“Go1”
A = Go2()  # 实例化“Go2”类
print(A.a)  # 打印实例属性“a”
A.do2()  # 调用实例方法“do2”
复制代码

For from ... import ... statement should be noted, has not been written in the import behind the content will not be imported

from test import hi # 从模块test中导入函数“hi”
hi()
# 以下语句将会导致报错,因为并没有导入test模块,只是导入test模块中的函数“hi”
test.hey()
复制代码

When we need to specify all of the content from modules directly used,

Can be written as [**] from xx module import * ** forms, * stands for "module in all variables, functions, classes"

from test import *
print(a)  # 打印变量“a”
hi()  # 调用函数“hi”
print(Go1.a)  # 打印类属性“a”
Go1.do1()  # 调用类方法“Go1”
A = Go2()  # 实例化“Go2”类
print(A.a)  # 打印实例属性“a”
A.do2()  # 调用实例方法“do2”
复制代码

Difference from xxx import * and the import xxx

if __name__ == '__main__'

__name__A built-in variable, the current file is displayed as__main__

If the py file, as a module, is introduced into other files, then __name__it becomes py file name

Let py file, both for their own use, but he ...

__name__Built-in variables

In the current file, its value __main__, the other being introduced as a module py file, its file name is

The introduction of other modules

import time # 引入的第三方模块
print('第一句话,过两秒出现第二句。')
time.sleep(2)
print('第二句话。')
复制代码

Use the pip

List all packages pip list

See which packages need to be upgraded pip list --outdated / pip list -o

pip pip uninstall uninstall package package name

Online installation pip install + module name

pip install SomePackage latest version

pip install SomePackage == 1.0.4 specified version

pip install 'SomePackage> = 1.0.4' minimum version

Pip install --upgrade module upgrade package name

pip accelerate

Path: C: \ Users \ Administrator \ pip \ pip.ini

content:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
复制代码

Installation WHL pip (offline installation)

www.lfd.uci.edu/~gohlke/pyt…

To download, install

Export installation package pip freeze> requirements.txt

astroid==2.2.5
autopep8==1.4.4
beautifulsoup4==4.7.1
certifi==2019.3.9
chardet==3.0.4
colorama==0.4.1
idna==2.8
isort==4.3.20
lazy-object-proxy==1.4.1
lxml==4.3.3
mccabe==0.6.1
natsort==6.0.0
pies==2.6.7
pprint==0.1
pycodestyle==2.5.0
pylint==2.3.1
PyMySQL==0.9.3
python-docx==0.8.10
reqeusts==1000.0.0
requests==2.22.0
six==1.12.0
soupsieve==1.9.1
typed-ast==1.3.5
urllib3==1.25.3
wrapt==1.11.1
复制代码

Installation package introduced pip install -r requirements.txt

How to learn a module?

View module file location

import random
print(random.__file__)
复制代码

When we from random import *, when we introduced what?

Read the official document https://docs.python.org/3.6/library/random.html

Or directly Baidu search

The use case code make notes ...

import random  # 调用random模块
a = random.random()  # 随机从0-1之间抽取一个小数
print(a) # 0.22
a = random.randint(0,100)  # 随机从0-100之间抽取一个数字
print(a) # 66
a = random.choice('abcdefg')  # 随机从字符串/列表/字典等对象中抽取一个元素(可能会重复)
print(a) # c
a = random.sample('abcdefg', 3) # 随机从字符串/列表/字典等对象中抽取多个不重复的元素
print(a) # ['d', 'a', 'e']
items = [1, 2, 3, 4, 5, 6]  # “随机洗牌”,比如打乱列表
random.shuffle(items)
print(items) # [3, 2, 6, 4, 1, 5]
复制代码

You can use ** dir () ** function to view a module to see if it contains any variables, functions, classes, class methods

Search for a specific function ... (seed)

Small job: self-study time and datetime modules, made the code notes, explain to everyone

Quick Jump:

Cat brother teach you to write reptile 000-- begins .md
cat brother teach you to write reptile 001 - print () functions and variables .md
cat brother teach you to write reptile 002-- job - Pikachu .md print
cat brother teach you to write reptiles 003 data type conversion .md
cat brother teach you to write reptile 004-- data type conversion - small practice .md
cat brother teach you to write reptile 005-- data type conversion - small jobs .md
cat brother teach you to write reptile 006- - conditional and nested conditions .md
cat brother teach you to write 007 reptile conditional and nested conditions - small operating .md
cat brother teach you to write reptile 008 - input () function .md
cat brother teach you to write reptiles 009 - input () function - AI little love students .md
cat brother teach you to write a list of 010 reptiles, dictionaries, circulation .md
cat brother teach you to write reptile 011-- lists, dictionaries, circulation - small jobs .md
cat brother teach you to write a Boolean value, and four reptile 012-- statements .md
cat brother teach you to write a Boolean value, and four reptile 013-- statements - smaller jobs .md
cat brother teach you to write reptile 014 - pk game. md
cat brother teach you to write reptile 015 - pk game (new revision) .md
cat brother teach you to write reptile 016-- function .md
cat brother teach you to write reptile 017-- function - a small job .md
cat brother to teach you write reptile 018--debug.md
cat brother teach you to write reptile 019 - debug- job. md
cat brother teach you to write reptiles 020-- Classes and Objects (on) .md
cat brother teach you to write reptiles 021-- Classes and Objects (a) - Job .md
Cat brother teach you to write reptiles 022-- Classes and Objects (lower) .md
cat brother teach you to write reptiles 023-- Classes and Objects (lower) - Job .md
cat brother teach you to write reptile 024-- decoding coded && .md
cat brother teach you to write reptile 025 && decoding coded - small jobs .md
cat brother teach you to write reptile 026-- module .md
cat brother teach you to write reptile 027-- module introduces .md
cat brother teach you to write reptile 028- - introduction module - small job - billboards .md
cat brother teach you to write Preliminary -requests.md reptile reptilian 029--
cat brother teach you to write reptile reptilian 030-- Preliminary -requests- job .md
cat brother teach you to write 031 reptiles - reptile basis -html.md
cat brother teach you to write reptile reptilian 032-- first experience -BeautifulSoup.md
cat brother teach you to write reptile reptilian 033-- first experience -BeautifulSoup- job .md
cat brother teach you to write reptile 034- - reptile -BeautifulSoup practice .md
cat brother teach you to write 035-- reptile reptilian -BeautifulSoup practice - job - film top250.md
cat brother teach you to write 036-- reptile reptilian -BeautifulSoup practice - work - work to resolve .md movie top250-
cat brother teach you to write 037-- reptile reptiles - to listen to songs .md baby
cat brother teach you to write reptile 038-- arguments request .md
cat brother teach you to write data stored reptile 039-- .md
cat brother teach you to write reptiles 040-- store data - Job .md
cat brother teach you to write reptile 041-- analog login -cookie.md
Cat brother teach you to write reptile 042 - session usage .md
cat brother teach you to write reptile 043-- analog browser .md
cat brother teach you to write reptile 044-- analog browser - job .md
cat brother teach you to write reptiles 045-- coroutine .md
cat brother teach you to write reptile 046-- coroutine - practice - what to eat not fat .md
cat brother teach you to write reptile 047 - scrapy framework .md
cat brother teach you to write reptile 048-- .md reptile reptiles and anti-
cat brother teach you to write reptile 049-- end Sahua .md

Reproduced in: https: //juejin.im/post/5cfc4ada6fb9a07ed657c5d2

Guess you like

Origin blog.csdn.net/weixin_33840661/article/details/91465264