저는 Python으로 클래스와 객체 지향을 공부하고 있는데 패키지에서 클래스를 가져올 때 문제가 발견되었습니다.Python 클래스 가져 오기 오류
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()
을 내가에서 를 사용하려고하면 ex1.pojo import Fone, 다음 오류 발생 :
fone = Fone(70)
TypeError: 'module' object is not callable
하지만 ex1.pojo.Fone 가져 오기 *에서 를 사용하는 경우, 프로그램이 잘 실행됩니다.
내가 코딩 한 방식으로 Fone 클래스를 가져올 수없는 이유는 무엇입니까? 당신이 할 때 파이썬에서
흠, 이상합니다. 저는 모듈 가져 오기가 구문의 측면에서 클래스 가져 오기와 같다고 생각했습니다. 이것은 Java에서 클래스를 생성하는 데 사용 되었기 때문에 혼란 스럽지만 Python은 .py 파일을 모듈로 취급합니다.이 때문에 모듈 이름이 대문자로 바뀌 었습니다. –
@ AndréCaldas 예, Python에서는 모듈에 여러 클래스 또는 여러 함수가 포함될 수 있습니다. 또는 혼합 – dm03514