Python:class方法未被同一类中的另一个方法调用

Python:class方法未被同一类中的另一个方法调用,python,methods,scope,Python,Methods,Scope,首先,我对Python没有太多的经验,所以如果这是一个过于简单的问题,我很抱歉。我试着在谷歌上搜索解决方案,结果一无所获 我目前正在一个加密货币交易平台上工作,该平台使用高速公路通过WAMP接收市场更新。但是,我的一个类方法有问题。有关守则如下: class TradeClient(ApplicationSession): exchange = "NULL" def eventProcessor(self, event): print("eventProc

首先,我对Python没有太多的经验,所以如果这是一个过于简单的问题,我很抱歉。我试着在谷歌上搜索解决方案,结果一无所获

我目前正在一个加密货币交易平台上工作,该平台使用高速公路通过WAMP接收市场更新。但是,我的一个类方法有问题。有关守则如下:

class TradeClient(ApplicationSession):
    exchange = "NULL"   

    def eventProcessor(self, event):
        print("eventProcessor called")
        print("market event received: {}".format(event))

    def onJoin(self, details):
        print("{} client session ready".format(exchange))

        def marketEvent(event):
            print("marketEvent called")
            self.processEvent(event)

            [...]
现在,很明显,TradeClient是一个打算成为超类的类,exchange变量和eventProcessor方法打算在子类中被覆盖,以便与不同的exchange兼容(用于套利等)。marketEvent方法被称为fine,但eventProcessor方法不是

这是一个范围问题吗?看起来似乎不应该这样,向eventProcessor传递event[:]的参数没有什么区别

编辑:

marketEvent函数嵌套在onJoin中。我为没有明确说明这是事实而道歉——我假设缩进会说明这一点。正确、完整的代码粘贴在下面。我希望调用eventProcessor的原因是为了使JSON的解析易于为不同的交换重写

from autobahn.asyncio.wamp import ApplicationSession
from autobahn.asyncio.wamp import ApplicationRunner
from asyncio import coroutine 
import sys
import json
from io import StringIO

poloniex_URL = "wss://api.poloniex.com"    

class TradeClient(ApplicationSession):

    exchange = "NULL"

    def eventProcessor(self, event):
        print("eventProcessor called")
        print("market event received: {}".format(event))

    def onJoin(self, details):
        print("{} client session ready".format(self.exchange))

        def marketEvent(event):
            print("marketEvent called")
            self.eventProcessor(self, event)

        # Read in configuration files
        try:
            pairs = [line.strip() for line in open("conf/" + self.exchange + ".conf")]
        except:
            print("Configuration file not found for {}!".format(self.exchange))
            sys.exit(1)

        # Subscribe to each currency pair / topic in the conf file
        for pair in pairs:
            try:
                yield from self.subscribe(marketEvent, pair)
                print("subscribed to {} on {}".format(pair, self.exchange))
            except Exception as e:
                print("could not subscribe to {} on {}: {}".format(pair, exchange, e))
                sys.exit(1)

class PoloniexClient(TradeClient):
    exchange = "poloniex"

poloniex_runner = ApplicationRunner(url = poloniex_URL, realm = "realm1")
poloniex_runner.run(PoloniexClient)

方法
marketEvent
的缩进错误。向左侧伸出4格。其次,在这个方法中调用了一个方法
processEvent
,这个方法以前没有定义过。您有
eventProcessor
,但您调用了
processEvent
。在这种情况下,marketEvent必须嵌套在onJoin中,否则它将不会被相关事件触发。另外,具有讽刺意味的是,我重命名了processEvent,试图使其更清晰,以便在此处发布。当名称匹配时,它仍然不起作用。