Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对象和分配给变量的函数之间的横向关联_Python_Oop - Fatal编程技术网

Python 对象和分配给变量的函数之间的横向关联

Python 对象和分配给变量的函数之间的横向关联,python,oop,Python,Oop,我定义了在子例程和子例程下演化的对象。我想通过探针来探测这些变量的状态 例如,让我们称这个类为醉鬼 class drunk (object): def __init__(self): self.position = 0 self.probability = 0.5 def take_a_step(): # code that will make the drunkard change its position 然后,我可能会产生许

我定义了在子例程和子例程下演化的对象。我想通过探针来探测这些变量的状态

例如,让我们称这个类为醉鬼

class drunk (object):
    def __init__(self):
        self.position = 0
        self.probability = 0.5
    def take_a_step():
        # code that will make the drunkard change its position
然后,我可能会产生许多醉汉,并把他们放在班级人群中

class crowd (object):
    def __init__(self):
        self.inidividuals = num.array([])
我想创建一个可以分配到任何这些对象中的探测对象,但我有实现问题

class probe (object):
    def __init__(self):
        self.function = None
        self.data = num.array([])
我想将探测器分配给任何醉汉,探测器将从人群或醉汉中提取在probe.function中的函数中定义的任何信息

但就我而言,我可以通过将探测放入这些类中的列表来分配探测,并通过关联类crowd和dunk方法中的函数来调用它们

但为了做到这一点

probe_mean = probe()
def probe_function():
    #
    #
    #
probe_mean.function = probe_function()
probe_mean = probe()
def probe_function(probed):
    return probed.position.mean()
probe_mean.function = probe_function(drunk_53)

drunk_53.probes.append(probe_mean)
但是,由于我想引用一个类中的变量,函数将位于该类的列表中,因此我必须将函数定义为

但为了做到这一点

probe_mean = probe()
def probe_function():
    #
    #
    #
probe_mean.function = probe_function()
probe_mean = probe()
def probe_function(probed):
    return probed.position.mean()
probe_mean.function = probe_function(drunk_53)

drunk_53.probes.append(probe_mean)
我有一种感觉,从事实上说,这些对象应该互相引用,以使其工作,这不是他应该的方式

我认为必须有一种方法来处理这个问题,而不必在函数中重新引入dunk_53,比如超级函数,这将实现垂直关联


我没有OOP方面的经验,所以我想用Pythonic、优雅等最合适的方式来实现这一点。你的意见是什么?您将采取什么方法?

您遗漏了必须将函数定义为的内容。你看了吗?是的,我把它漏掉了,因为我不知道如何在不将群组对象按定义反馈回函数的情况下实现它。我会检查你的链接,谢谢。