【发布时间】:2013-05-18 23:35:59
【问题描述】:
我正在研究 python 中的类和 OO,当我尝试从包中导入类时发现了一个问题。项目结构和类描述如下:
ex1/
__init__.py
app/
__init__.py
App1.py
pojo/
__init__.py
Fone.py
类:
Fone.py
class Fone(object):
def __init__(self,volume):
self.change_volume(volume)
def get_volume(self):
return self.__volume
def change_volume(self,volume):
if volume >100:
self.__volume = 100
elif volume <0:
self.__volume = 0
else:
self.__volume = volume
volume = property(get_volume,change_volume)
App1.py
from ex1.pojo import Fone
if __name__ == '__main__':
fone = Fone(70)
print fone.volume
fone.change_volume(110)
print fone.get_volume()
fone.change_volume(-12)
print fone.get_volume()
fone.volume = -90
print fone.volume
fone.change_volume(fone.get_volume() **2)
print fone.get_volume()
当我尝试使用 from ex1.pojo import Fone 时,会出现以下错误:
fone = Fone(70)
TypeError: 'module' object is not callable
但是当我使用 from ex1.pojo.Fone import * 时,程序运行良好。
为什么我不能用我编码的方式导入 Fone 类?
【问题讨论】: