2010-11-23 3 views
1

私はちょうどPythonで始まっています。私はエラーに遭遇しました。私は修正する方法がわかりません。以下のコード例を参照してください。私は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

関連する問題