为什么 class "QuoteClientFactory" 没有被理解为在 pycharm 中定义?

How come the class "QuoteClientFactory" is not being understood as Defined in pycharm?

我正在使用 o'REilys Twisted 网络编程必备指南学习网络编程。 (使用 pycharm IDE)

我有两个问题,函数 maybeStopReactor() 在 pycharm 中未被识别,QuoteClientFactory 未被视为已定义 class。

我怎样才能找到解决这个问题的方法?

class QuoteClientFactory(protocol.ClientFactory):
    def __init__(self, quote):
        self.quote = quote

    def buildProtocol(self, addr):
        return QuoteProtocol(self)

    def clientConnectionFailed(self, connector, reason):
        print("connecton failed:"), reason.getErrorMessage()
        **maybeStopReactor()**

    def clientConnectionLost(self, connector, reason):
        print("connection lost"), reason.getErrorMessage()
        maybeStopReactor()

    def maybeStopReactor(self):
        global quote_counter
        quote_counter -=1
        if not quote_counter:
            reactor.stop()

    quotes = [
        "you snooze you lose",
        "The early bird gets the worm",
        "carpe diem"
    ]

    quote_counter = len(quotes)

    for quote in quotes:
        **reactor.connectTCP('localhost', 6942, QuoteClientFactory(quote))**
    reactor.run()

你的缩进有误。有点难看,因为代码跨越了一个分页符。你想要的是:

class QuoteClientFactory(protocol.ClientFactory):
    def __init__(self, quote):
        self.quote = quote

    def buildProtocol(self, addr):
        return QuoteProtocol(self)

    def clientConnectionFailed(self, connector, reason):
        print("connecton failed:"), reason.getErrorMessage()
        maybeStopReactor()

    def clientConnectionLost(self, connector, reason):
        print("connection lost"), reason.getErrorMessage()
        maybeStopReactor()

def maybeStopReactor():
    global quote_counter
    quote_counter -=1
    if not quote_counter:
        reactor.stop()

quotes = [
    "you snooze you lose",
    "The early bird gets the worm",
    "carpe diem"
]

quote_counter = len(quotes)

for quote in quotes:
    reactor.connectTCP('localhost', 6942, QuoteClientFactory(quote))
reactor.run()