程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python learning 10 -- engineering structure (package, module) & namespace & import module and variable &_ init_. py&_ all_&_ name_

編輯:Python

1、 Engineering structure ( package 、 modular )

Distinguish between directories and packages :

If you have this document , It's just a bag ; If there is no , That's the catalog .

modular :xxxx.py file

2、 Namespace


A variable with the same name as a global variable in a function becomes a local variable

To reference a global variable , The function must be modified

Local namespace :

Global namespace :


locals() read-only
globals() read-write

3、 stay python Import modules into the project

1)


2)
The module name you want to import is too long

names


There is another import method ( Dynamic import )

It's fine too :
from package import moudule
When this package When it comes to hierarchical packages , To write out the parent package .

4、 Import the variables

Import a variable from another module , It can also be nicknamed

The code of the imported module will also be executed

When importing multiple variables , It can be enclosed in brackets , Line break . So that we don't make a mistake .

It can also be used. * To import all variables

5、init.py

  • The directory where the flag is located is a module package
  • It's also a module in itself , It can also be executed like other modules
  • Can be used to define what will be imported when fuzzy import
  • When importing a package , Will execute _init_.py What's in it

6、all

  • Put it in init Inside , It will mark the module in fuzzy import
  • Put it under the common module , Identify a module , Which attributes are allowed to be imported into other modules ( Fuzzy import )

7、name

Displays the name of the current module in the execution project , If the current program is running in this module , This module is called _main_

if name=="main" Generally refers to the entrance where the whole project starts to operate

Determine whether it is in this module , if , The output ……, otherwise , Output the name of this module


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved