Python基础--模块


你的程序、代码就是一个模块。

Python中我们用import来带入模块,也就类似于C++中include某个头文件。

>>>improt math
>>>math.sin(0)

导入自己的模块:

#hello.py
print "Hello world!"

将上面的文件保存在c:/python下面:

import sys
sys.path.append('c:/python')

>>>import hello
Hello world!

记住只导入一次

>>>import hello
Hello world!

>>>import hello
#空

模块用于定义函数:

#hello2.py
def hello():
  print "Hello, world!"

>>>import hello2
>>>hello2.hello()
Hello, world!

下面就是一些杂项:


为了更好的组织模块,可以将他们分组,即包。

import contants
print contants.PI

help帮助

>>>help(copy.copy)

评论关闭