在python中调用另一个对象的方法中的对象属性

时间:2018-04-29 18:12:12

标签: python function oop object instance

我正在制作一个MLP并将我最后的隐藏层连接到输出。

class connection:
    def __init__(self):
        self.value = 0
        self.weight = 0
        self.network_output_index = 0
def connect(self,**network_instance**):
    **network_instance**.output_array[self.network_output_index] = self.value * self.weight

class network:
    def __init__(self):
        self.output_array = [0,0,0,0]
    blah blah python blah;

如何调用network_instance的属性output_array

1 个答案:

答案 0 :(得分:1)

修复缩进问题后,这似乎没有问题:

class connection:
    def __init__(self):
        self.value = 0
        self.weight = 0
        self.network_output_index = 0

    def connect(self, network_instance):
        network_instance.output_array[self.network_output_index] = self.value * self.weight

class network:
    def __init__(self):
        self.output_array = [0,0,0,0]

c = connection()
c.value = 5
c.weight = 2

n = network()

c.connect(n)

print(n.output_array)

[10, 0, 0, 0]