调用类中的嵌套函数

2024-09-28 01:23:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在测试一个小功能,barbar中的manage()函数调用call_central,其中包含函数call2和call3。 我想在call_central上创建一个对象,以便在需要时调用call2call3。你知道吗

一些背景:我打算做的是。在我的程序中,call2正在实例化另一个类,而call3正在使用此信息执行其他各种操作。你知道吗

编辑:更多详细信息: 我创建嵌套函数的基础如下。假设您正在尝试执行一些调度工作并测试各种调度程序。barbar函数将从您要实现的“所有”调度算法中获取所有参数。每个调度算法本身都有类。在这个MWE中,它被称为ThisClass。“call\u central”所做的是在call2中实例化这个特定的调度算法,并使用它在call3中实际调度

有几个问题:

  1. 为什么它要我把“自我”当作一个论点。你知道吗
  2. 为什么s(self).call2()返回None
  3. 有没有办法避免为此使用嵌套函数?你知道吗

代码:

#!/usr/bin/env python3

class CallThisClass:
    def __init__(self):
        pass
    def do_something_here(self):
        while True:
            print ("doing something")

class CallThisClass1:
    def __init__(self):
        pass
    def do_something_here(self):
        while True:
            print ("doing something")


class barbar:
    def __init__(self, which_function=None):
        print ("do nothing")
        self.which_function = which_function
        self.manage()

    def manage(self):
        FUNCTIONS = globals()['barbar']
        s  = getattr(FUNCTIONS, self.which_function)
        print (s(self))#.call2())

    def call_central(self):
        print ("in call central")
        def call2():
            self.invoke_callthisclass = CallThisClass()
            print ("do nothing from call2")
        def call3():
            self.invoke_callthisclass.do_something_here()
            print ("do nothing from call3")
            return 0

    def call_central2(self):
        print ("in call central")
        def call4():
            self.invoke_callthisclass1 = CallThisClass1()
            print ("do nothing from call4")
        def call5():
            self.invoke_callthisclass1.do_something_here()
            print ("do nothing from call5")
            return 0


d = barbar(which_function="call_central")

Tags: 函数selfwhichdeffunctioncall调度do
1条回答
网友
1楼 · 发布于 2024-09-28 01:23:08

你在做一些很奇怪的事情,但我会尽量给出一些合理的答案。你知道吗

为什么它要我把“自我”当作一个论据。

您的代码如下所示:

def manage(self):
    FUNCTIONS = globals()['barbar']
    s  = getattr(FUNCTIONS, "call_central")
    print (s(self))#.call2())

与其通过globalsdict,不如等价地编写s = barbar.call_centralbarbar.call_central是一个未绑定的方法。这意味着它是一个实例级方法,没有任何实例可供调用。你知道吗

作为Python中对象如何工作的快速迂回,让我们讨论一个更简单的类:

class Foo(object):
  def bar(self):
    pass

f = Foo()
print(Foo.bar) # => "<unbound method Foo.bar>"
print(f.bar) # => "bound method Foo.bar of <__main__.Foo object at 0x...>>"

所以,f是类Foo的一个实例。bar是一个实例方法-Foo的实例可以调用这个方法。Foo.bar没有绑定到任何实例;要调用它,它需要一个Foo的实例。f.bar是方法Foo.bar,绑定到实例f。所有这些绑定意味着,无论何时调用该方法,绑定实例都会作为第一个参数传入。这意味着f.bar()Foo.bar(f)做基本相同的事情。你知道吗

那么,为什么需要将self作为参数传递给barbar.call_central?因为barbar.call_central是一个未绑定的实例方法,需要调用一个实例。self.call_central是绑定方法,您可能需要调用它。getattr(self, self.which_function)将为您提供绑定方法。你知道吗

为什么s(self).call2()返回None

这是call2的定义:

def call2():
    self.invoke_callthisclass = CallThisClass()
    print ("do nothing from call2")

没有return,执行只会从末尾掉下来。当执行从函数的末尾停止时,它返回None。你知道吗

你写过这样的东西:

def call2():
    self.invoke_callthisclass = CallThisClass()
    print ("do nothing from call2")
def call3():
    self.invoke_callthisclass.do_something_here()
    print ("do nothing from call3")
    return 0

因为您取消了indent,所以离开了call2的范围,所以下面的行都与该函数无关。值得指出的是,call3也永远不会return 0,因为您的do_something_here方法永远不会返回。你知道吗

有没有办法避免为此使用嵌套函数?

据我所知,你的问题似乎就是你想做的:

class BaseScheduler(object):
    def do_the_thing(self):
        raise NotImplementedError("bla bla bla please define a method in {}", type(self))

class Scheduler1(BaseScheduler):
    pass

class Scheduler2(BaseScheduler):
    pass

class BarBarOrWhatever(object):
    def __init__(self, scheduler_cls):
        avaialble_schedulers = BaseScheduler.__subclasses__()
        # Iterate over available_schedulers and do whatever setup you wanted here
        self.__scheduler = scheduler_cls()
        self.__scheduler.do_the_thing()

BarBarOrWhatever(Scheduler1)

您说过您希望__init__方法对所有可用的调度程序执行某些操作,所以__sublcasses__位就是这样做的。参见:
How to find all the subclasses of a class given its name?。其他的一切都很简单。你知道吗

相关问题 更多 >

    热门问题