python中package机制的两种实现方式
当执行import
module时,解释器会根据下面的搜索路径,搜索module1.py文件。
1) 当前工作目录
2) PYTHONPATH中的目录
3) Python安装目录
(/usr/local/lib/python)
事实上,模块搜索是在保存在sys.path这个全局变量中的目录列表中进行搜索。
sys.path会在解释器开始执行时被初始化成包含:
1)当前工作目录
2) PYTHONPATH中的目录
3) Python安装目录
(/usr/local/lib/python)
package是模块的集合,每一个Package的根目录下面都应当有一个__init__.py
文件。当解释器发现目录下有这个文件时,他就会认为这是一个Package,而不是一个普通的目录。
我们通过下面这样的一个实例来说明
假定项目结构如下:
demo.py
MyPackage
---classOne.py
---classTwo.py
---__init__.py 现在我们通过两种方式来实现包机制,主要区别就在于是否在__init__.py中写入模块导入语句。
1,__init__.py是一个空白文件的方式, demo.py内容如下:
from MyPackage.classOne import classOne
from MyPackage.classTwo import classTwo
if __name__ == "__main__":
c1 = classOne()
c1.printInfo()
c2 = classTwo()
c2.printInfo()
classOne.py内容如下:
class classOne:
def __init__(self):
self.name = "class one"
def printInfo(self):
print("i am class One!")
classTwo.py内容如下:
class classTwo:
def __init__(self):
self.name = "class two"
def printInfo(self):
print("i am class two!")
2,如果在__init__.py中写入导入模块的语句,则上述例子可以这样来做。
其中__init__.py
相关文档:
万恶的编码
小菜对于老师上一节讲的不是很明白,因为没有一本书是将文件与web一起讲授的,他决定自己探究一下它们之间的不同:
首先,小菜在C盘建了一个文本文档 file.txt,输入四个字:我是小菜。
然后,小菜在shell中练习起来:
>>> file=open("c:\\file.txt","r")
>>> data=file.read()
>> ......
队列:
与堆栈类似,通过python的列表类型来实现,参考 help(list)
shoplist=['apple','mango','carrot','banana']
print 'I have',len(shoplist),'items to purchase'
print 'these items are:'
for item in shoplist:
print item,
shoplist.append('rice')
print 'my shopping list is now', shoplist
shoplist. ......
list.append(item)
list.extend(sequence)
http://docs.python.org/tutorial/datastructures.html
http://docs.python.org/library/functions.html 这几天看一下
python howto
恩。python documentation 确实很好很强大啊!
list.append(x)Add an item to the end of the list; equivalent to a[len(a):]&n ......
python
语言概览
python
脚本可以处理外部传进来的参数 即sys.argv[]
,argv[]
的使用与linux
下相同
python
本身是解释语言,可以对输入的式子求值。python
支持的对象如整数都是立即数,此外他支持复数,及对四则运算解释。
ptyhon
支持字符串,放在单/
双引号内,字符串是数组,可以通过[i: ......
refer from: http://www.daniweb.com/forums/thread115282.html#
python
Syntax
(Toggle Plain Text
)
# respond to a key
without the need to press
enter
import
Tkinter
as tk
def
keypress(
event)
:
if
event.keysym
== 'Escape'
:
root.destroy
......