day8: Python articles function using the learning function parameter type (and package management module)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43264177/article/details/102759778

A, and a packet management module
1. The module defines and packages
(1) modules (Moudle): Python module is a file, ending .py, containing Python Python object definitions and statements.
(2) package (Package): Python package is a directory that contains a __init__.py file (folder).
2. The role of the module
(1) module allows you to organize your Python code segment logically
(2) the relevant function code writes a module to make your code easier to use, more understandable.
(3) the module can be defined functions, variables, and classes, where the module can also contain executable code.
Here Insert Picture Description
3. Module import mode:
Here Insert Picture Description

import *
import.. as..
from...import...
from...import *
from...import...as

Precautions: In addition to the top-level directory, layer by layer peel your heart

import sys
print(sys.path)	# 返回的是一个列表
# @time:2019/10/26 18:44
# @Author:coco
# @File:test01.py
# @software:PyCharm

import random

a = 100

num = random.random()
print(num)


def work():
    print('test01中的work函数')

# 魔法变量:__name__:
print('__name__的值:  ',__name__)
# 在模块导入的时候不希望执行部分内容
if __name__ == "__main__":
    num = random.random()
    print(num)

operation result:
Here Insert Picture Description

# @time:2019/10/26 19:39
# @Author:coco
# @File:test02.py
# @software:PyCharm

""""
为什么代码中会有浅色的波浪线和红色的波浪线
浅色的波浪线:pycharm监测到你的这行代码不符合pop8规范(官方给出的编码规范)
红色的波浪线:pycharm监测到这行代码语法有问题或者变量找不到(没有被定义)
"""
# 方式一:导入整个模块
# import test01
#
# print(test01.a)
# test01.work()

from test01 import a  # 导入时会把test01中的模块从上到下执行

print(a)

# 方式二:导入模块中的部分内容
from test01 import work  # 这个波浪线并不是代码写错了,只是pycharm识别不出我们自定义的这个模块

work()

# 扩展
# 通过as给导入进来的方法或变量,重新起名字(别名)
from test01 import work as w1

w1()


def work():
    print('test02中的work')


work()
w1()

import test01 as t1

print(t1.a)
t1.work()

# 导入多个内容
from test01 import a, work, num

print(a)
print(num)

# 导入模块中所有的内容(不推荐使用)
from test01 import *
print(a)
print(num)
work()

operation result:
Here Insert Picture Description

# @time:2019/10/26 20:11
# @Author:coco
# @File:test03.py
# @software:PyCharm

# 包导入

# 方式一
# from 包名 import 模块名
from pack01 import module1

module1.m_func1()
print(module1.aa)
print(module1.bb)

# 方式二:
#  from 包名.模块名 import 模块中的变量或方法
from pack01.module1 import aa, bb

print(aa)
print(bb)

# 剥洋葱
# from pack01.ppp import p_m1
# from pack01.ppp.p_m1 import 变量或方法

The result:
Here Insert Picture Description
the search path 4. imported modules

# @time:2019/10/26 20:30
# @Author:coco
# @File:p_m1.py
# @software:PyCharm

import sys

print(sys.path)

for i in sys.path:
    print(i)

from py23_07day import zy_06day

# 这些路径是我们在导入包的时候,python解释器在搜索我们导入的包
# 从上往下搜索
# 检测不到第一条,第一条是我们运行文件所在的路径
"""
C:\Python37\python.exe D:/Python_test/py23_class/py23_08day/test011/p_m1.py
['D:\\Python_test\\py23_class\\py23_08day\\test011', 'D:\\Python_test\\py23_class', 'C:\\Python37\\python37.zip', 'C:\\Python37\\DLLs', 'C:\\Python37\\lib', 'C:\\Python37', 'C:\\Python37\\lib\\site-packages']
D:\Python_test\py23_class\py23_08day\test011
D:\Python_test\py23_class
C:\Python37\python37.zip
C:\Python37\DLLs
C:\Python37\lib
C:\Python37
C:\Python37\lib\site-packages
"""

operation result:
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43264177/article/details/102759778