Python Object-Oriented---module

Source: Internet
Author: User

Goal
    • Module
    • Package
    • Production of modules
01, Concept of Module 1.1 module

Note module is a core concept of the Python program architecture

    • Each py python source code file that ends with an extension is a module
    • The module name is also an identifier and requires a naming convention that conforms to the identifier
    • The global variables, functions, and classes defined in the module are tools that are provided for direct use by the outside world.
    • module is like a toolkit , you want to use the tool in this toolkit, you need to import this module first
1.2 Two ways to import a module 1,import import
import 模块名1, 模块名2  (不推荐)

tip : When importing a module, each import should be exclusive of one row

import 模块名1import 模块名2
    • After import
      • by 模块名. using the tools provided by the module--Global variables, functions, classes
Use as to specify the alias of the module

Note : If the name of the module is too long , you can use as the name of the specified module to make it easier to use in your code

import 模块名1 as 模块别名

Note : module aliases should conform to the large hump naming method

2, from ... import imports
    • If you want to import parts of a tool from a module , you can use it in a from ... import way that
    • import 模块名is to import all the tools in the module at once , and access through the module name/alias
# 从 模块 导入 某一个工具from 模块名1 import 工具名
    • After import
      • no need to pass模块名
      • You can directly use the tools provided by the module--Global variables, functions, classes

Attention
If there are two modules with a function with the same name , then the function that imports the module will overwrite the previously imported function

    • Development-time import code should be written uniformly at the top of the code to make it easier to find conflicts in time
    • Once a conflict occurs, you can use the as keyword to start an alias for one of the tools
From...import *
# 从 模块 导入 所有工具from 模块名1 import *

Attention
This method is not recommended, because the name of the function does not have any hint, the problem is not good to troubleshoot

1.3 Search Order of modules

pythonWhen importing a module , the interpreter will:
1, search the current directory to specify the module name of the file, if there is a direct import
2, if not, then search the system directory

Note : In development, name the file, do not duplicate the system module file name

pythonEach module is a built-in property __file__ to view the full path of the module

Example

import random# 生成一个 0 ~ 10 的数字rand = random.randint(0, 10)print(rand)

Note : If there is a file in the current directory, the random.py program will not perform properly

    • At this time, python the interpreter will load the current directory random.py without loading the system's ' random module
1.4 Principles--Each file should be imported
    • A separate python file is a module .
    • When you import a file, all code in the file that does not have any indentation will be executed again
Actual development scenarios
    • In the actual development, each module is independently developed, most of them are responsible for
    • developers usually add some test code below the module
      • Used only within the module and imported into other files does not need to be executed
nameProperty
    • __name__Property can be done, the code of the test module is run only in the test case and will not be executed on import
    • __name__Is python a built-in property that records a string
    • If it is imported by another file , it __name__ is the module name
    • If the program is currently executing , __name__ it is__main__
In manypythonYou will see code in the following format in the file:
# 导入模块# 定义全局变量# 定义类# 定义函数# 在代码的最下方def main():    # 测试代码...    pass# 根据 __name__ 判断是否执行下方代码if __name__ == "__main__":    main()
02, Package Concept
    • A package is a special directory that contains multiple modules
    • There is a special file under the directory__init__.py
    • The package name is named in the same way as the variable name , lowercase +_

Benefits

    • Use import 包名 to import all modules in a package at once
Case walkthroughs

1, create a new hm_message package
2, in the directory, create a new two files send_message andreceive_message
3, send_message define a function in the file send
4, receive_message set a function in the file receive
5. Packages that are imported directly on hm_message the outside

Init. py
    • To use a module in a package outside the world, you need to specify a list of modules that are __init__.py provided to the outside world
# 从 当前目录 导入 模块列表from . import send_messagefrom . import receive_message
def receive():    return "这是来自 110 的短信"
def send(text):    print("正在发送 %s" % text)
03, Release module
    • If you want to create your own module and share it with others, you can follow these steps:
3.1 Making a release compress package step

Directory structure

[[email protected] python_code]# tree.├── hm_message│   ├── __init__.py│   ├── receive_message.py│   └── send_message.py└── setup.py
1, create setup.py
    • setup.pyof the file
[[email protected] python_code]# more setup.pyfrom distutils.core import setupsetup(name="String.hm_message", # 包名    version="1.0", # 版本    description="itheima‘s 发送和接收消息模块", #描述信息    long_description="完整的发送和接收消息模块", #完整描述信息    author="itheima", # 作者    author_email="[email protected]", # 作者邮件    url="www.itheima.com", # 主页    py_modules=["hm_message.send_message", "hm_message.receive_message"])
2, building the module
[[email protected] python_code]# python3 setup.py buildrunning buildrunning build_pycreating buildcreating build/libcreating build/lib/hm_messagecopying hm_message/__init__.py -> build/lib/hm_messagecopying hm_message/send_message.py -> build/lib/hm_messagecopying hm_message/receive_message.py -> build/lib/hm_message[[email protected] python_code]# tree.├── build│   └── lib│       └── hm_message│           ├── __init__.py│           ├── receive_message.py│           └── send_message.py├── hm_message│   ├── __init__.py│   ├── receive_message.py│   └── send_message.py└── setup.py4 directories, 7 files
3, generate a release package
[[email protected] python_code]# python3 setup.py sdistrunning sdistrunning checkwarning:sdist:manifest Template ' manifest.in ' does not exist (using the default file list) Warning:sdist:standard file not Found:should has one of the README, README.txt, readme.rstwriting manifest file ' manifest ' creating string.hm_message-1.0creating string.hm_message-1.0/ Hm_messagemaking hard links in String.hm_message-1.0...hard linking setup.py string.hm_message-1.0hard linking Hm_ message/__init__.py, String.hm_message-1.0/hm_messagehard linking hm_message/receive_message.py, STRING.HM _message-1.0/hm_messagehard linking hm_message/send_message.py, string.hm_message-1.0/hm_messagecreating Distcreating tar archiveremoving ' string.hm_message-1.0 ' (and everything under it) [[email protected] python_code]         # tree.├──build│  └──lib│  └──hm_message│  ├──__init__.py│   ├──receive_message.py│  └──send_message.py├──dist│  └──string.hm_message-1.0.tar.gz├──hm_message│  ├──__init__.py│   ├──receive_message.py│  └──send_message.py├──manifest└──setup.py5 directories, 9 files

note to make that version of the module, use which version of the interpreter to execute

3.2 Installing the module
[[email protected] dist]# tar zxvf string.hm_message-1.0.tar.gzstring.hm_message-1.0/string.hm_message-1.0/ Pkg-infostring.hm_message-1.0/hm_message/string.hm_message-1.0/hm_message/__init__.pystring.hm_message-1.0/hm_ message/receive_message.pystring.hm_message-1.0/hm_message/send_message.pystring.hm_message-1.0/setup.py[[ Email protected] dist]# tree.├──string.hm_message-1.0│  ├──hm_message│  │   ├──__init__.py│  │  ├──receive_message.py│  │  └──send_message.py│   ├──PKG-INFO│  └──SETUP.PY└──STRING.HM_MESSAGE-1.0.TAR.GZ2 directories, 6 files[[email  Protected] dist]# CD string.hm_message-1.0[[email protected] string.hm_message-1.0]# python3 setup.py Installrunning installrunning buildrunning build_pycreating buildcreating build/libcreating build/lib/hm_ Messagecopying hm_message/__init__.py, build/lib/hm_messagecopying hm_message/send_message.py, Build/lib/hm_messagecopying hm_message/receive_message.py, build/lib/hm_messagerunning install_libcreating/usr/local/ Python3/lib/python3.7/site-packages/hm_messagecopying build/lib/hm_message/__init__.py-/usr/local/python3/ Lib/python3.7/site-packages/hm_messagecopying build/lib/hm_message/send_message.py-/usr/local/python3/lib/ Python3.7/site-packages/hm_messagecopying build/lib/hm_message/receive_message.py-/usr/local/python3/lib/ Python3.7/site-packages/hm_messagebyte-compiling/usr/local/python3/lib/python3.7/site-packages/hm_message/__ init__.py to __init__.cpython-37.pycbyte-compiling/usr/local/python3/lib/python3.7/site-packages/hm_message/send _message.py to Send_message.cpython-37.pycbyte-compiling/usr/local/python3/lib/python3.7/site-packages/hm_ message/receive_message.py to Receive_message.cpython-37.pycrunning Install_egg_infowriting/usr/local/python3/lib /python3.7/site-packages/string.hm_message-1.0-py3.7.egg-info[[email protected] String.hm_message-1.0]# Cd/usr/local/python3/lib/python3.7/site-packages/[[email protected] site-packages]# Tree hm_message/hm_ Message/├──__init__.py├──__pycache__│  ├──__init__.cpython-37.pyc│  ├──receive_ Message.cpython-37.pyc│  └──send_message.cpython-37.pyc├──receive_message.py└──send_message.py1 Directory, 6 files[[email protected] site-packages]# Ipython3python 3.7.0 (Default, Jul 2018, 16:50:30) Type ' copy Right ', ' credits ' or ' license ' for more Informationipython 6.4.0 – an enhanced Interactive Python. Type '? ' for help. In [1]: Import Hm_messagein [2]: Hm_message.send_message.send ("Hello") is being sent Helloin [3]: Hm_message.receive_ Message.receive () out[3]: ' This is a text message from 110 '
Uninstalling the module

Directly from the installation directory, the directory of the installation module can be deleted

[[email protected] String.hm_message-1.0]# cd /usr/local/python3/lib/python3.7/site-packages/[[email protected] site-packages]# rm -f hm_message*
3.3 Pip installing a third-party module
    • third-party modules usually refer to packages/modules developed by well-known third-party teams and widely used by programmers python
      • For example pygame , a very mature game development module
    • pipis a modern, universal python package management tool
    • Provides the python ability to find, download, install and uninstall packages.
Install and uninstall commands into the following:
# 将模块安装到 Python 2.x 环境$ sudo pip install pygame$ sudo pip uninstall pygame# 将模块安装到 Python 3.x 环境$ sudo pip3 install pygame$ sudo pip3 uninstall pygame

Python Object-Oriented---module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.