Why is python so awesome? Is there a lot of modules and frameworks? The most detailed tutorials

Source: Internet
Author: User

Overview

Module Brief

Python__name__

Module Benefits

    • Improved maintainability of the code
    • Improved code consumption, when a module is complete, can be referenced in multiple places
    • Conflicts between function names and variable names can be avoided

    • ARGV: Get a list of command-line arguments
    • Import SYS introduces the sys.py module in the Python standard library
    • Import: This is the method of introducing a module (mentioned later)
    • Sys.path: Contains a list of paths for the Python interpreter to automatically find the required modules

Custom Modules

    • Not only can we use third-party modules and system modules in Python, we can also use custom modules to
    • In Python, a. py file is a module
    • Here is a titan.py module I have customized, the code is as follows

It is important to note that:

    • A module will only be imported once, no matter how many times you execute the import, you can prevent the module from being repeatedly referenced
    • When introducing any module (including custom modules), do not add the. py suffix
    • When we use the import statement, how does the Python interpreter find the corresponding file?
    • This involves Python's search path, which is made up of a list of directory names, in which the Python interpreter looks for the modules introduced.
    • This looks much like an environment variable, in fact, you can also determine the search path by defining the environment variables.
    • The search path is determined when the Python is compiled or installed, and the installation of the new library should also be modified. Path variables stored in the SYS module by the search paths

Using the example

It is important to note that:

This approach may result in the same name as the method or variable defined in this document, which should be noted here

From...import* Way

Pour all the contents of a module into the current namespace, but it's best not to use it too much

# 格式:from modname import *# 使用:from Titan import *sayGood()print(age)

Module built-in properties and functions

__name__ Property

.py__name__if __name__ == ‘__main__‘: print(‘这是Titan模块‘)else: def sayGood(): print(‘good‘) def sayNice(): print(‘nice‘) def sayBad(): print(‘bad‘) age = 20 name = ‘titan‘
    • Both the name and main are double-underlined
    • Each module has a __name__ attribute that, when its value equals __main__, indicates that the module itself is executing, otherwise it is introduced to other files
    • If the current file is a portal file for the program, the value of the __name__ property is __main__

Dir () function

dir()dir()import Titanprint(dir(Titan))# 输出结果:[‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘age‘, ‘name‘, ‘sayBad‘, ‘sayGood‘, ‘sayNice‘]print(dir())# 输出结果:[‘Titan‘, ‘__annotations__‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘]# 这里定义一个新的变量sum = 30print(dir())# 输出结果:[‘Titan‘, ‘__annotations__‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘, ‘sum‘]# 把定义的变量删除后del sumprint(dir())# 输出结果:[‘Titan‘, ‘__annotations__‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘]

Package

Here we first consider the question: in the same project there are two or more than two developers have defined the same name of the module (of course, the lion in different directories, the same directory is not possible to create the same name of the file), then how to call the module

Solve

    • In order to solve the above-mentioned module naming conflicts, the method of organizing modules according to the directory is introduced, which becomes the package
    • A package is a form of managing a Python module namespace, using the "Point module name"
    • Once the package is introduced, the module will not conflict with others as long as the top-level package does not collide with others
    • The so-called top-level package refers to the previous level of the file directory
    • The same package can contain multiple modules
    • For example, a module named A.B represents a sub-module named B in a package named A.

It is important to note that:

Within each package (the module's sibling directory) must create a file named __init__.py, mainly to avoid some impostors name, currently the file can be nothing to write:

Calling methods

import a.Titanimport b.Titanimport b.codera.Titan.sayGood()b.Titan.sayGood()b.coder.sayGood()# 输出结果:good--agood--bgood--coder

Installing using third-party modules

About PIP

    • Third-party libraries are installed and managed by PIP in Python, which is responsible for installing and managing third-party libraries like pods in IOS
    • In Mac and Linux system PIP is installed by default, generally do not need to reinstall, if there is a problem, please Baidu to solve
    • In the MAC system there will be a default version of Python2.7, I installed a version of 3.6, natural default also installed Pip
    • Here are some commands for PIP in Python3.6, which need to be executed with PIP3 as the command header
# 查看当前pip版本pip3 -V# 安装第三方库pip3 install ...# 对pip进行升级pip3 install --upgrade pip3

Install third-party libraries

    • MAC system installed third-party library, directly open the terminal to execute the installation command
    • Pillow is already the Python platform de facto image processing standard library.
    • PIL is very powerful, but the API is very easy to use
pip3 install Pillow

Use of third-party modules

Manipulating images

The following is the most common example code for image scaling operations

from PIL import Image# 打开一个jpg图像文件,注意是当前路径:im = Image.open(‘titan.jpg‘)# 获得图像尺寸w, h = im.sizeprint(‘image size: %sx%s‘ % (w, h))# 缩放到50%:im.thumbnail((w//2, h//2))print(‘image to: %sx%s‘ % (w//2, h//2))# 把缩放后的图像用jpeg格式保存:im.save(‘jun.jpg‘, ‘jpeg‘)

Other features such as slices, rotations, filters, output text, color palettes, and more are readily listed below:

from PIL import Image, ImageFilter# 打开一个jpg图像文件,注意是当前路径:im = Image.open(‘jun.jpg‘)# 应用模糊滤镜:im2 = im.filter(ImageFilter.BLUR)im2.save(‘jun1.jpg‘, ‘jpeg‘)

Welcome to my public number or blog park: Python Learning Exchange https://home.cnblogs.com/u/Python1234/

Welcome to my thousand People Exchange Learning Group: 125240963

Why is python so awesome? Is there a lot of modules and frameworks? The most detailed tutorials

Related Article

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.