学习笔记37-‘xxx‘ object has no attribute ‘xxx‘ 及 ‘xxx‘ takes 2 positional arguments but 3 were given报错原因

一晚上遇到两常见的小错误,分享一下!

AttributeError: ‘xxx’ object has no attribute ‘xxx’

明明已经定义了,代码仍然出错!!!
原因1:int()双下划线的忽略(当__使用双下划线时会变为紫色为正确)
在这里插入图片描述
原因2:路径中有中文。

TypeError: xxx takes 2 positional arguments but 3 were given

原因:传参问题。注意:self表示创建的类实例本身,所以在内部就可以把各种属性绑定到self。在创建实例的时候,就不能传入空的参数,必须传入与方法匹配的参数。但注意self不需要传,Python解释器会自己把实例变量传进去。

错误演示:

class ABC:
...
    def channel(self, x):
    	b, c, h, w = x.shape 
        x = x.reshape(b, 2, -1, h, w)
        return x
	
	def forward(self, x):
		...
		out = self.channel(out, 2)#传了2个参数,报错!
		return

正确演示:

class ABC:
...
    def channel(self, x, group):
    	b, c, h, w = x.shape 
        x = x.reshape(b, group, -1, h, w)
        return x
	
	def forward(self, x):
		...
		out = self.channel(out, 2)#传了2个参数,x=out,group=2
		return

或者

class ABC:
...
    def channel(self, x, group=2):
    	b, c, h, w = x.shape 
        x = x.reshape(b, group, -1, h, w)
        return x
	
	def forward(self, x):
		...
		out = self.channel(out)#传了1个参数,x=out
		return
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值