Python学习笔记 之 Python程序组织

在Python中组织程序,组织的方法有两种吧?使用模块,或者使用包


1. 使用模块组织程序:
导入模块方法:
import sys;

将系统的sys模块导入到shell中。在你将代码编写为独立运行的程序时,可能需要自动导入一些公用模块,可以如下形式写文件。

#!/usr/bin/env python3.1
# program file demonstration
import sys;

第一行用于在Linux或Unix下使用python,说明本文件要调用python进行解释。

创建模块:
模块其实就是一个py文件,文件名称也即模块名。例如创建School.py文件,那么对应的该模块的名称为School。
在使用时,通过将School导入,即可使用其中的变量或类,import School;

使用模块中类,例如Student,创建一个对象。
stu = School.Student(20);

如果要去掉School的前缀名,则可以使用如下的导入方式,将Student导入进来。
from School import Student;

注:在导入模块时,模块所在的目录需要在系统的寻找目录中,也即包含在sys.path列表中
使用list(sys.path); 可以查看path中包含的目录名称。如果不包含在该列表中,使用列表的方法
将目录加入到path中。

完整例子:
School模块中包含了Student和Teacher两个类,代码如下:
# modules School


class Student:
"""This is a class of student, presents a student.
Atributes:
sid   : student id in school, int
name : str
sex  : str
age  : int
sclass: int
Methods:
getName()       return name of a student object
setName( name);      Set the name of a student object
setInfo(name, sex, age, class); Set student infomation
"""
def __init__( self, sid):
self.sid = sid;
return ;


def getName(self):
return self.name;


def setName( self, name):
self.name = name;
return ;


def setInfo(self, name, sex, age, sclass):
self.name = name;
self.sex = sex;
self.age = age;
self.sclass = sclass;
return ;

class Teacher:
"""Teacher class uses to store teacher's infomation.
Atributes:
tid: teacher id
name:
sex:
cource:


methods:
getName(): get the name of a teacher object
setName(): change the name of a teacher object
"""
def __init__(self, tid):
self.tid = tid;
return ;
def getName(self):
return self.name;
def setName( self, name):
self.name = name;
return ;


def setCourse( self, course):
self.course = course;
return ;
def getCourse(self):
return self.cource;

判断是否包含目录:
>>> import sys
>>> list(sys.path);
['C:\\Python32\\Lib\\idlelib', 'C:\\WINDOWS\\system32\\python32.zip', 'C:\\Python32\\DLLs', 'C:\\Python32\\lib', 
'C:\\Python32', 'C:\\Python32\\lib\\site-packages']

显然不包含我们的目录("F:\\PythonLearn"),加入我们的目录:
>>> sys.path.append("F:\\PythonLearn");


再次查看一下:
>>> list(sys.path);
['C:\\Python32\\Lib\\idlelib', 'C:\\WINDOWS\\system32\\python32.zip', 'C:\\Python32\\DLLs', 'C:\\Python32\\lib', 
'C:\\Python32', 'C:\\Python32\\lib\\site-packages', 'F:\\PythonLearn']

引入School模块:
>>> import School
>>> stu = School.Student(20);
>>> stu.setName("guotao");
>>> name = stu.getName();
>>> name
'guotao'

直接引入Student类
>>> from School import Student;
>>> stu1 = Student(1);
>>> stu1.setName("guo");
>>> name = stu1.getName();
>>> name
'guo'

调用模块的方法:
>>> lt = School.getList();
>>> lt
['Teacher', 'Student']
直接引入函数名称:
>>> from School import getList;
>>> li = getList();
>>> li
['Teacher', 'Student']


将模块所有的内容引入到当前作用域:
From School import *;


2. 使用包组织模块:

一般单独的类在单独的文件中更加有用,一个文件中有多个类,容易陷入组织问题。
为了解决将一个类放入一个文件中,Python提供了包,包类似于操作系统中的目录,或文件夹。使得同一个目录下的问题在一起使用时就像单独的模块。

我们将上面的模块进行分解,转换为包的组织形式,
首先创建名为SchoolPack的目录,将所有的类单独一个文件进行放置,Student.py,Teacher.py,CommonMethod.py。此外还需要一个__init__.py文件
__init__文件中包含了控制包的用法的代码,与模块不同,目录中的每个文件在包导入时没有立即导入,相反,__init__.py文件首先执行,它里面指定了
要使用的文件以及使用它们的方式。

注:在导入包时,Python只执行__init__.py,没有这个文件,我们无法看到包中任何文件。

错误演示:

>>> import SchoolPack
>>> SchoolPack.__name__
'SchoolPack'
>>> Stu = SchoolPack.Student(20); // 直接使用Student错误
Traceback (most recent call last):
 File "<pyshell#5>", line 1, in <module>
Stu = SchoolPack.Student(20);
AttributeError: 'module' object has no attribute 'Student'

>>> import SchoolPack.Student; // 需要将Student所在的模块导入进来,再使用Students
>>> Stu = SchoolPack.Student.Student(20);
>>> Stu.setName("guo");
>>> na = Stu.getName();
>>> na
'guo'
注:从这一点看,包的概念要大于模块的概念。包比模块要大一级别。


为了让所有的类可用,需要显示将import语句放入__init__.py中。
from SchoolPack import Student;
from SchoolPack import Teacher;
from SchoolPack import CommonMethods;

在其中加入 __all__=["Student", "Teacher"];,使用from SchoolPack import *; 即可将所有的all列表中的类导入。

此方法暂时不正确,不知何处出错。



另外一种导入包中对象或模块的方法是,逐级写出目录的名称
例如:
>>> from SchoolPack.Student import Student;
>>> stu = Student(20);
>>> stu.setName("g");
>>> n = stu.getName();
>>> n
'g'


3. 在sys.modules中存储了当前加载模块的名称,


通过'SchoolPack' in sys.modules 可以判断该模块或包是否已经加载,如果加载可以使用 sys.modules.pop("SchoolPack");方法删除掉已加载的模块或包。

或者使用 reload(SchoolPack);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值