2010-11-23 2 views
1

나는 파이썬으로 시작하고 오류를 해결하기 위해 수정하는 방법을 모른다. 아래 코드 예제를 참조하십시오. event_timer.py을 실행하면 다음과 같은 오류 메시지가 나타납니다. 아래 나열된 두 파일 모두 같은 폴더에 있습니다.모듈을 연결할 수 없습니까? 확실하지 않은 방법

 
Traceback (most recent call last): 
    File "E:\python\event_timer\event_timer.py", line 7, in 
    timer = EventTimer() 
TypeError: 'module' object is not callable 

누군가 내가 누락 된 내용을 말해 줄 수 있습니까?

event_timer.py :

 
import EventTimer 

timer = EventTimer() 

timer.addStep("Preheat Oven", seconds = 10) 
timer.addStep("Cook Pizza", seconds = 20) 
timer.addStep("Done!") 

timer.start() 

EventTimer.py :

 
import time 

class Timer: 

    event = 'Event' 
    steps = [] 

    def __init__(self, event = None): 

     if event is not None: 

      self.event = event 

    def addStep(self, step, seconds = None, minutes = None, hours = None, days = None): 

     if seconds is not None: 

      unit = 'seconds' 
      amount = seconds 

     elif minutes is not None: 

      unit = 'minutes' 
      amount = minutes 

     elif hours is not None: 

      unit = 'hours' 
      amount = hours 

     elif days is not None: 

      unit = 'days' 
      amount = days 

     else: 

      print 'Invalid arguments' 

      return False 

     self.steps.append({'unit': unit, 'amount': amount}) 

     return True 

    def __timeInSeconds(self, unit, amount): 

     if unit == 'seconds': 

      return amount 

     elif unit == 'minutes': 

      return amount * 60 

     elif unit == 'hours': 

      return amount * 60 * 60 

     elif unit == 'days': 

      return amount * 60 * 60 * 24 

     else: 

      print 'Invalid unit' 

      return False 

    def start(self): 

     if len(self.steps) == 0: 

      print 'No steps to complete' 

      return False 

     print "{0} has started.".format(self.event) 

     for step in self.steps: 

      print step.step 

      time.sleep(self.__timeInSeconds(step.unit, step.amount)) 

      print "Completed" 

     print 'Event complete' 

답변

10

당신은

import EventTimer 

당신이 가리키는 새로운 변수, EventTimer을 쓸 때 모듈 - 방금 작성한 모듈! 해당 모듈 내부에는 클래스 Timer이 있습니다. 따라서 해당 클래스의 인스턴스를 만들려면

timer = EventTimer.Timer() 
+0

또한 코드를 다음과 같이 변경하십시오. from EventTimer import Timer; timer = Timer()' – hughdbrown

+1

@hughdbrown : 가능하지만, 거의 유용하지 않고 때로는 위험하거나 불필요하게 장황하다. – delnan

+0

사실, 나는 OP가 그렇게해야한다는 것을 의미했습니다. 하지만 우리가 그 위에있는 동안 위험 해? 가져올 항목을 지정하고 최소화하는 데 거의 도움이되지 않습니까? – hughdbrown

관련 문제