试图理解功能论证

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (49)

我知道这可能是一个非常类型的问题,但我仍然无法理解函数式编程,并且有一个我无法理解的特定代码片段:

完整代码,但遗漏了大部分函数定义:

import blpapi
import sys

SESSION_STARTED         = blpapi.Name("SessionStarted")
SESSION_STARTUP_FAILURE = blpapi.Name("SessionStartupFailure")
SERVICE_OPENED          = blpapi.Name("ServiceOpened")
SERVICE_OPEN_FAILURE    = blpapi.Name("ServiceOpenFailure")
ERROR_INFO              = blpapi.Name("ErrorInfo")
GET_FILLS_RESPONSE      = blpapi.Name("GetFillsResponse")

d_service="//blp/emsx.history"
d_host="localhost"
d_port=8194
bEnd=False

class SessionEventHandler():

    def processEvent(self, event, session):
        try:
            if event.eventType() == blpapi.Event.SESSION_STATUS:
                self.processSessionStatusEvent(event,session)

            elif event.eventType() == blpapi.Event.SERVICE_STATUS:
                self.processServiceStatusEvent(event,session)

            elif event.eventType() == blpapi.Event.RESPONSE:
                self.processResponseEvent(event)

            else:
                self.processMiscEvents(event)

        except:
            print ("Exception:  %s" % sys.exc_info()[0])

        return False


    def processSessionStatusEvent(self,event,session):
        print ("Processing SESSION_STATUS event")

        for msg in event:
            pass

    def processServiceStatusEvent(self,event,session):
        print ("Processing SERVICE_STATUS event")

        for msg in event:
            pass

    def processResponseEvent(self, event):
        print ("Processing RESPONSE event")

        for msg in event:

            global bEnd
            bEnd = True

    def processMiscEvents(self, event):

        print ("Processing " + event.eventType() + " event")

        for msg in event:

            print ("MESSAGE: %s" % (msg.tostring()))


def main():

    sessionOptions = blpapi.SessionOptions()
    sessionOptions.setServerHost(d_host)
    sessionOptions.setServerPort(d_port)

    print ("Connecting to %s:%d" % (d_host,d_port))

    eventHandler = SessionEventHandler()

    session = blpapi.Session(sessionOptions, eventHandler.processEvent)

    if not session.startAsync():
        print ("Failed to start session.")
        return

    global bEnd
    while bEnd==False:
        pass

    session.stop()

我可以按照这里的代码进行操作:

session = blpapi.Session(sessionOptions, eventHandler.processEvent)

在这里,我看到我正在从blpapi库调用“Session”,并传递一些选项以及我的eventHandler.processEvent。这是我迷路的地方。我看一下这个特定的功能,看看:

def processEvent(self, event, session):
    try:
        if event.eventType() == blpapi.Event.SESSION_STATUS:
            self.processSessionStatusEvent(event,session)

        elif event.eventType() == blpapi.Event.SERVICE_STATUS:
            self.processServiceStatusEvent(event,session)

        elif event.eventType() == blpapi.Event.RESPONSE:
            self.processResponseEvent(event)

        else:
            self.processMiscEvents(event)

    except:
        print ("Exception:  %s" % sys.exc_info()[0])

    return False

我看到该函数试图辨别传递了什么类型的事件,并将根据该事件类型在类中执行不同的函数。麻烦的是,我无法弄清楚事件的指定位置!“事件”来自哪里?我将其视为该特定函数中的参数,但没有将事件参数传递给:

session = blpapi.Session(sessionOptions, eventHandler.processEvent)

那么它如何知道此时该做什么?这个“事件”对象是如何神奇地出现的?

感谢您对我愚蠢的问题进行娱乐

提问于
用户回答回答于
session = blpapi.Session(sessionOptions, eventHandler.processEvent)

请注意,此后processEvent缺少括号()。这意味着您将函数本身作为参数传递给Session类。稍后将processEvent使用适当的参数调用此类。

边注:

我仍然无法理解函数式编程

“函数式编程”有一个非常具体的定义,这个例子不是它。如果您有兴趣,可以谷歌“函数式编程”或阅读维基百科文章以了解更多信息。但是,在您的学习过程中,这在现阶段并不重要。

扫码关注云+社区

领取腾讯云代金券