You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

实现NOR逻辑神经网络时遇TypeError:nor_NN()不接受参数

问题分析与解决

错误原因

你把实例化类和调用方法的代码缩进在了nor_NN类的内部,导致Python在解析类的过程中就尝试执行实例化操作。此时类还未完全定义完成,这种写法会被当作类的属性初始化逻辑,进而触发TypeError: nor_NN() takes no arguments错误。

修正后的代码

import numpy as np

class nor_NN:
    def __init__(self, iv):
        self.input_vector = np.array(iv)
        self.weight_vector = np.array([[0.5, -1, -1]])
        self.output = np.array([])

    def compute(self):
        mvf = np.c_[np.ones(4), self.input_vector]
        result = mvf.dot(self.weight_vector.transpose())
        self.output = [0 if r < 0 else 1 for r in result]

    def show_result(self):
        print("Input vector : \n", self.input_vector)
        print("Output : \n", self.output)

    def get_output(self):
        return self.output

# 把实例化和方法调用移到类外部,取消缩进
n = nor_NN(np.array([[0,0], [0,1], [1,0], [1,1]]))
n.compute()
n.show_result()

核心说明

Python里,类定义中缩进的代码属于类的成员(方法或属性),而创建类实例、调用实例方法的代码必须放在类定义的外部。这样才能确保类完全定义后,正常执行实例化和方法调用逻辑。

内容的提问来源于stack exchange,提问作者Shubham Bakal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 18:16:15