简体   繁体   English

函数内部函数最好的方法?

[英]Function inside a function best approach?

I have the following code below. 我在下面有以下代码。 I would like to execute additional C1 Class methods inside handleResponseEvent() when the event is fired. 我想在触发事件时在handleResponseEvent()内执行额外的C1类方法。 Experimenting I found that if I define handleResponseEvent() inside main() it does what I want. 试验我发现,如果我在main()定义handleResponseEvent() main()它会做我想要的。 I was wondering if there are other ways of doing the same thing and if defining the event handler function inside main() was the best approach. 我想知道是否还有其他方法可以做同样的事情,如果在main()定义事件处理函数是最好的方法。

def main():
    C1 = DNPClass()
    C1.method1(arg1, arg2, arg3, arg4, arg5)

    # subscribing to event
    C1.method2.RequestEvent += handleResponseEvent
    ...

def handleResponseEvent( request, response):
    #code to execute when event handler is called
    #code references additional method from C1 instance
    ....

Defining a function inside of another function where you are using the local variables of the outer function makes perfect sense, particularly when you are passing out the inner function itself to another part of code, like you do with event subscription. 在使用外部函数的局部变量的另一个函数内部定义函数非常有意义,特别是当您将内部函数本身传递给代码的另一部分时,就像使用事件订阅一样。

However, looking at your code, there already seems to be a direct relationship between the object you need and the event. 但是,查看代码时,您需要的对象与事件之间似乎存在直接关系。 I see C1.method2.RequestEvent , so it looks like RequestEvent could easily have a reference to C1 . 我看到C1.method2.RequestEvent ,所以看起来RequestEvent很容易引用C1 If that is the case, then it could pass that into the handler, where the handler looks something like this: 如果是这种情况,那么它可以将其传递给处理程序,其中处理程序看起来像这样:

def handleResponseEvent(request, response, dnp):

where dnp is the DNPClass instance referenced by the event ( C1 ). 其中dnp是事件( C1 )引用的DNPClass实例。 It's also possible that request or response already refer back to the DNPClass , so you wouldn't need the extra argument. requestresponse也可能已经引用回DNPClass ,因此您不需要额外的参数。

This would allow you to subscribe using the same handler regardless of the instance. 这将允许您使用相同的处理程序进行订阅,而不管实例如何。 For example: 例如:

def main():
    C1 = DNPClass()
    C1.method1(arg1, arg2, arg3, arg4, arg5)

    # subscribing to event
    C1.method2.RequestEvent += handleResponseEvent  # will call C1.method3

    C2 = DNPClass()

    C2.method2.RequestEvent += handleResponseEvent  # will call C2.method3


def handleResponseEvent(request, response):
    #code to execute when event handler is called
    #code references additional method from current DNPClass instance
    request.dnp.method3()
    ...

If this doesn't work, and it's not feasible to edit the DNPClass code to make it work, then I would at least confine this to a single function that accepts the instance so you don't need to define a new inner function each time. 如果这不起作用,并且编辑DNPClass代码以使其工作是不可行的,那么我至少会将其限制为接受实例的单个函数,因此您不需要每次都定义新的内部函数。 Something like this: 像这样的东西:

def subscribe_handleResponseEvent(dnp):
    def handleResponseEvent(request, response):
        #code to execute when event handler is called
        #code references additional method from dnp instance
        dnp.method3()
        ...

    dnp.method2.RequestEvent += handleResponseEvent


def main():
    C1 = DNPClass()
    C1.method1(arg1, arg2, arg3, arg4, arg5)

    # subscribing to event
    subscribe_handleResponseEvent(C1)
    ...

If handleResponseEvent is not used anywhere else, it might as well be in main . 如果handleResponseEvent没有在其他任何地方使用,它也可能在main It's probably better than making C1 global. 这可能比让C1全球化更好。 You could also pass C1 as a parameter (you'd have to change the signature of handleResponseEvent and modify method2.RequestEvent ). 您还可以传递C1作为参数(您必须更改handleResponseEvent的签名并修改method2.RequestEvent )。

The only issue I would take with this approach is that I wouldn't do that in the main function, otherwise it is perfectly valid. 我采用这种方法的唯一问题是我不会在main函数中这样做,否则它是完全有效的。

This is what I would call a functional approach; 这就是我所说的功能性方法; an OOP approach would make handleResponseEvent a method of the class DPNClass , so that the code would look like 一个OOP方法会使handleResponseEvent成为类DPNClass一个方法,这样代码看起来就像

def main():
    C1 = DNPClass()
    C1.method1(arg1, arg2, arg3, arg4, arg5)

    # subscribing to event
    C1.method2.RequestEvent += C1.handleResponseEvent
    ...

class DPNClass(...):
    ...
    def handleResponseEvent(self, request, response):
        #code to execute when event handler is called
        #code references additional method from self instance
        ....

This is of course possible only if you the class DPNClass is yours and you can modify it; 这当然是可能的,只有你的DPNClass类是你的,你可以修改它; otherwise you can create your own class and wrap C1 there. 否则你可以创建自己的类并在那里包装C1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM