#定义类
class A():
def __init__(self,a,b):
self.a = a
self.b = b
def add(self):
return self.a +self.b
#类的继承
class B():
def sub(self,a,b):
return a-b
#同一目录层级下的模组调用
import time
#采用import方式调用
print time.ctime()
from time import ctime #采用from ... import...方式调用
print ctime()
from time import * #一次性把模组下的所有方法都引用进来
print ctime()
sleep(2)
#调用下层中的模组(对应的模组在当前.py文件的下面1到多层目录中)
例子:
from model.pub import add #这种情况下必须在/model、目录下创建一个__init__.py文件,文件 #的内容可以为空
#调用上层的模组或者其他目录下的模组
具体结构:
Pythonwork
test1
ll
test2
引用实例:
import sys
sys.path.append("D:\Pythonwork") #设置临时环境变量为test1(test1为上层或者其 #其它层的模组)所在的目录的绝对路径
import test1 #调用test1
a = test1.A()
print a.add(4,8)
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。