gpt4 book ai didi

python - 模块在 Python 中不可调用?

转载 作者:太空宇宙 更新时间:2023-11-04 07:04:06 24 4
gpt4 key购买 nike

我遇到了 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

What am I missing?

event_timer.py:

import EventTimer

timer = EventTimer()

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

timer.start()

事件定时器.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'

最佳答案

当你写作时

import EventTimer

您创建了一个新变量,EventTimer,指向一个模块——您刚刚编写的模块!该模块内部是一个类 Timer。所以要创建该类的实例,您可以

timer = EventTimer.Timer()

关于python - 模块在 Python 中不可调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4259180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com