Use Python packages and modules

Working with packages and modules, you can make code more clear, different functions in different packages, package which also contains different function modules

Directory structure looks
a.py
b.py
in this directory is a directory test1, test1 below the directory has
__init__.py
ta.py
tb.py

test1 directory is a package of meaning, .py file is the module, there are some classes and functions

 

# -*- coding: cp936 -*-
#直接导入模块
import a
import b
print(a.a1(2))
print(a.a2(2))

print(b.b1(2))
print(b.b2(2))

#导入包里的模块
from test1 import ta
from test1 import tb
print(dir(ta))
print(dir(tb))


print(ta.a1(10))
print(ta.a2(10))

print(tb.b1(10))
print(tb.b2(10))

#一些基础东西
import os,sys
print(os.getcwd())#当前目录
print(os.chdir("E:\\"))#更改目录
print(os.getcwd())#当前目录
print(sys.platform)
print(sys.path)
print(chr(65))#A 把ASCII数值转换成字符
print(ord('a'))#97 把字符转换为ASCII数值
print(oct(8))#010 把整数转换成八进制
print(hex(255))#0xff 把整数转换成十六进制

The above operating results as follows: 

2
12 is
20 is
40
packets initialization
[ 'the __builtins__', 'the __doc__', '__FILE__', 'the __name__', '__PACKAGE__', 'A1', 'A2']
[ 'the __builtins__', 'the __doc__', '__FILE__', 'the __name__ ',' __PACKAGE__ ',' B1 ',' B2 ']
TA under test1 package: 10
TA under test1 package: 20 is
TB under test1 package: 100
TB under test1 package: 200 is
D: \ Python27
None
E: \
Win32
[ 'D: Lib \\ \\ \\ Python27 idlelib', 'C: \\ \\ python27.zip the WINDOWS system32 \\', 'D: \\ \\ Python27 DLLs',' D: \\ Python27 \ \ lib ',' D: \\ Python27 \\ lib \\ plat-win ',' D: \\ Python27 \\ lib \\ lib-tk ',' D: \\ Python27 ',' D: \\ Python27 \\ lib \\ site-packages']
Of
97
010
0xff

 

Published 46 original articles · won praise 9 · views 3632

Guess you like

Origin blog.csdn.net/weixin_41896770/article/details/105100584