Python module all in one
Python Modules
https://docs.python.org/3/tutorial/modules.html
Fibonacc
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while a < n:
result.append(a)
a, b = b, a+b
return result
# 導入模塊
import fibo
# 使用
fibo.fib(1000)
fibo.fib2(100)
print(fibo.__name__)
# 'fibo'
# 導入模塊
import fibo
# 重命名
fib = fibo.fib
# 使用
fib(1000)
fib2(100)
print(fibo.__name__)
# 'fibo'
# import語句有一個變體,可以將名稱從模塊直接導入到導入模塊的符號表中。
from fibo import fib, fib2
fib(500)
# 不存在 fibo, 沒有導入
print(fibo.__name__)
# None
# 這將導入除以下划線(_)開頭的名稱以外的所有名稱。 👎⚠️
from fibo import *
fib(500)
# 如果模塊名稱后跟as,則名稱后跟as直接綁定到導入的模塊。
import fibo as fib
# Alias
fib.fib(500)
print(fibo.__name__)
# None
from fibo import fib as fibonacci
fibonacci(500)
execute module
# python3 fibo.py <arguments>
$ python3 fibo.py
Module => Script ??? "main"
from fibo import fib as fibonacci
fibonacci(500)
# 您可以將文件用作腳本以及可導入模塊
if __name__ == "__main__":
import sys
fib(int(sys.argv[1]))
# 因為解析命令行的代碼僅在模塊作為“主”文件執行時運行:
$ python fibo.py 50
# 如果導入模塊,則代碼不會運行
# import fibo
sys.path
https://docs.python.org/3/library/sys.html#sys.path
Python 3 模塊導入方式
-
import Module
-
import Module as Alias_Module
-
from Module import Function
# coding: utf8
# import Module
# 導入整個模塊
import camelcase
模塊名.函數名
c = camelcase.CamelCase()
txt = "hello world"
print(c.hump(txt))
# coding: utf8
# import Module as Alias_Module
# 導入整個模塊, 並且使用 Alias 模塊別名
import camelcase as cc
c = cc.CamelCase()
txt = "hello world"
print(c.hump(txt))
# coding: utf8
# from Module import Function
# 導入部分模塊
from camelcase import CamelCase
函數名
c = CamelCase()
txt = "hello world"
print(c.hump(txt))
import group modules
python
# import group modules
from spider import url_manager, html_downloader, html_parser, html_outputer
# 導入部分模塊
from spider import url_manager
from spider import html_downloader
from spider import html_parser
from spider import html_outputer
create a module
# greeting_module.py
def greeting(name):
print("Hello, " + name)
use a module
Note: When using a
function
from a module, use the syntax:module_name.function_name
.
import greeting_module
greeting_module.greeting("xgqfrms")
# Hello, xgqfrms
import Module as Alias
module alias
human.py
person = {
"name": "xgqfrms",
"age": 23,
"country": "China"
}
import human as man
age = man.person["age"]
print(age)
# 23
import Module as Alias_Module
# coding: utf8
# 導入整個模塊
# import camelcase
# 模塊名.函數名
# c = camelcase.CamelCase()
# txt = "hello world"
# print(c.hump(txt))
# 導入部分模塊
# from camelcase import CamelCase
# 函數名
# c = CamelCase()
# txt = "hello world"
# print(c.hump(txt))
# import Module as Alias_Module
# 導入整個模塊, 並且使用 Alias 模塊別名
import camelcase as cc
c = cc.CamelCase()
txt = "hello world"
print(c.hump(txt))
"""bug
# ModuleNotFoundError: No module named 'Camelcase'
# AttributeError: module 'camelcase' has no attribute 'CamelCase'
# This method capitalizes the first letter of each word.
"""
Built-in Modules
import platform
x = platform.system()
print(x)
list function names in a module
list all the function names (or variable names) in a module
Note: The dir() function can be used on all modules, also the ones you create yourself.
#!/usr/bin/env python3
# coding: utf8
__author__ = 'xgqfrms'
__editor__ = 'vscode'
__version__ = '1.0.1'
__copyright__ = """
Copyright (c) 2012-2050, xgqfrms; mailto:xgqfrms@xgqfrms.xyz
"""
"""
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-01-0
*
* @description
* @augments
* @example
* @link
*
*/
"""
import platform
dir = dir(platform)
# List all the defined names belonging to the `platform` module:
print(dir)
# ['DEV_NULL', '_UNIXCONFDIR', '_WIN32_CLIENT_RELEASES', '_WIN32_SERVER_RELEASES', '__builtins__', '__cached__', '__copyright__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_comparable_version', '_component_re', '_default_architecture', '_dist_try_harder', '_follow_symlinks', '_ironpython26_sys_version_parser', '_ironpython_sys_version_parser', '_java_getprop', '_libc_search', '_linux_distribution', '_lsb_release_version', '_mac_ver_xml', '_node', '_norm_version', '_parse_release_file', '_platform', '_platform_cache', '_pypy_sys_version_parser', '_release_filename', '_release_version', '_supported_dists', '_sys_version', '_sys_version_cache', '_sys_version_parser', '_syscmd_file', '_syscmd_uname', '_syscmd_ver', '_uname_cache', '_ver_output', '_ver_stages', 'architecture', 'collections', 'dist', 'java_ver', 'libc_ver', 'linux_distribution', 'mac_ver', 'machine', 'node', 'os', 'platform', 'popen', 'processor', 'python_branch', 'python_build', 'python_compiler', 'python_implementation', 'python_revision', 'python_version', 'python_version_tuple', 're', 'release', 'subprocess', 'sys', 'system', 'system_alias', 'uname', 'uname_result', 'version', 'warnings', 'win32_ver']
from Module import Function
You can choose to
import only parts
from a module, by using thefrom
keyword.
test_module.py
# test_module has one function and one dictionary:
def greeting(name):
print("Hello, " + name)
person = {
"name": "xgqfrms",
"age": 23,
"country": "China"
}
from test_module import person
print (person["age"])
Note: When importing using the from
keyword, do not use the module name
when referring to elements in the module.
camelcase bug
https://www.w3schools.com/python/showpython.asp?filename=demo_camelcase
import camelcase
c = camelcase.CamelCase()
txt = "hello world"
print(c.hump(txt))
# Hello World
refs
https://www.w3schools.com/python/python_modules.asp
https://www.runoob.com/python3/python3-module.html
©xgqfrms 2012-2020
www.cnblogs.com 發布文章使用:只允許注冊用戶才可以訪問!