Python __init__ 问题:必须使用 Bank 实例作为第一个参数调用未绑定方法 __init__() (改为使用 int 实例)

发布于 2024-08-24 09:21:15 字数 2805 浏览 7 评论 0原文

class Teller(object):
    def __init__(self):
        self.occupied = False
        self.timeLeft = 0
        self.totTime

    def occupy(self, timeOcc):
        self.occupied = True
        self.timeLeft = timeOcc

    def nextMin(self):
        self.timeLeft -= 1
        self.totTime += 1
        if self.timeLeft == 0:
            self.occupied = False

class Bank(object):
    def __init__(numTellers, hoursOpen):
        self.tellers = []
        self.timeWaited = 0
        self.clientsWaiting = []
        for x in xrange(numTellers):
            tempTeller = Teller.__init__()
            self.tellers.append(tempTeller)
        self.minutesOpen = hoursOpen * 60

    def tellerOpen(self):
        for x in xrange(len(self.tellers)):
            if not self.tellers[x].occupied:
                return x+1
        return 0

    def runSim(self, queueInput):  #queueInput is a list of tuples (time, timeAtTeller)
        simTime = self.minutesOpen
        totCli = 0
        timeToNext = queueInput[0][0]
        timeAtNext = queueInput[0][1]
        queueInput.pop(0)
        self.clientsWaiting.append([timeToNext, timeAtNext])

        while simTime > 0:
            for person in self.clientsWaiting:
                if person[0]:
                    person -= 1
            if not self.clientsWaiting[len(self.clientsWaiting)-1][0]:
                timeToNext = queueInput[0][0]
                timeAtNext = queueInput[0][1]
                queueInput.pop(0)
                self.clientsWaiting.append([timeToNext, timeAtNext])

            remove = 0
            for x in xrange (len(self.clientsWaiting)-1):
                if tellerOpen() and not self.clientsWaiting[x][0]:
                    self.tellers[tellerOpen()].occupy(self.clientsWaiting[x][0])
                    totCli += 1
                    remove += 1
                elif not tellerOpen() and not self.clientsWaiting[x][0]:
                    self.timeWaited += 1

            for x in xrange(remove):
                self.clientsWaiting.pop(x)

            print """The total time spent in the queue by all clients was %d minutes. The total number of clients today was %d. The average waiting time was %d mins""" % (self.timeWaited, totCli, self.timeWaited / totCli)\



    if __name__ == '__main__':
    inp = raw_input()
    tList = inp.split('\n')
    qList = []
    for item in tList:
        tList = item.split(' ')
        qList.append((tList[0], tList[1]))

    virtBank = Bank.__init__(3, 7)
    bank.runSim(qList)

这导致了这个错误:

> TypeError: unbound method __init__() must be called with Bank instance as first argument (got int instance instead)

我看不出我没有做错什么。任何建议将不胜感激。

我认为唯一重要的部分是 Bank 类 __init__ 和调用 virtBank = Bank.__init__(3, 7)

class Teller(object):
    def __init__(self):
        self.occupied = False
        self.timeLeft = 0
        self.totTime

    def occupy(self, timeOcc):
        self.occupied = True
        self.timeLeft = timeOcc

    def nextMin(self):
        self.timeLeft -= 1
        self.totTime += 1
        if self.timeLeft == 0:
            self.occupied = False

class Bank(object):
    def __init__(numTellers, hoursOpen):
        self.tellers = []
        self.timeWaited = 0
        self.clientsWaiting = []
        for x in xrange(numTellers):
            tempTeller = Teller.__init__()
            self.tellers.append(tempTeller)
        self.minutesOpen = hoursOpen * 60

    def tellerOpen(self):
        for x in xrange(len(self.tellers)):
            if not self.tellers[x].occupied:
                return x+1
        return 0

    def runSim(self, queueInput):  #queueInput is a list of tuples (time, timeAtTeller)
        simTime = self.minutesOpen
        totCli = 0
        timeToNext = queueInput[0][0]
        timeAtNext = queueInput[0][1]
        queueInput.pop(0)
        self.clientsWaiting.append([timeToNext, timeAtNext])

        while simTime > 0:
            for person in self.clientsWaiting:
                if person[0]:
                    person -= 1
            if not self.clientsWaiting[len(self.clientsWaiting)-1][0]:
                timeToNext = queueInput[0][0]
                timeAtNext = queueInput[0][1]
                queueInput.pop(0)
                self.clientsWaiting.append([timeToNext, timeAtNext])

            remove = 0
            for x in xrange (len(self.clientsWaiting)-1):
                if tellerOpen() and not self.clientsWaiting[x][0]:
                    self.tellers[tellerOpen()].occupy(self.clientsWaiting[x][0])
                    totCli += 1
                    remove += 1
                elif not tellerOpen() and not self.clientsWaiting[x][0]:
                    self.timeWaited += 1

            for x in xrange(remove):
                self.clientsWaiting.pop(x)

            print """The total time spent in the queue by all clients was %d minutes. The total number of clients today was %d. The average waiting time was %d mins""" % (self.timeWaited, totCli, self.timeWaited / totCli)\



    if __name__ == '__main__':
    inp = raw_input()
    tList = inp.split('\n')
    qList = []
    for item in tList:
        tList = item.split(' ')
        qList.append((tList[0], tList[1]))

    virtBank = Bank.__init__(3, 7)
    bank.runSim(qList)

This results in this error:

> TypeError: unbound method __init__() must be called with Bank instance as first argument (got int instance instead)

I don't see what I've dont wrong. Any advice would be appreciated.

The only important parts, I think, are the Bank class __init__ and the call virtBank = Bank.__init__(3, 7)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我是男神闪亮亮 2024-08-31 09:21:15

这里有两点需要注意:

  1. 你不应该直接调用__init__,它是一个神奇的方法,当你构造这样的对象时会调用它:

    virtBank = 银行(3, 7)
    
  2. 实例被隐式传递给构造函数,但必须明确接收,如下所示:

    def __init__(self, numTellers, hoursOpen):
        # ...
    

2 points to make here:

  1. You shouldn't be calling __init__ directly, it's a magic method which is invoked when you construct an object like this:

    virtBank = Bank(3, 7)
    
  2. The instance is implicitly passed to the constructor, but it must be explicitly received, like this:

    def __init__(self, numTellers, hoursOpen):
        # ...
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文