'xxx' object has no attribute 'xxx' and 'xxx' takes 2 positional arguments but 3 were given error reason

I encountered two common mistakes in one night, and I want to share them!

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

Although it has been defined, the code still fails! ! !
Reason 1: Int () double underline is ignored (it will turn purple when __ uses double underline, which is correct)
Insert image description here
Reason 2: There is Chinese in the path.

TypeError: xxx takes 2 positional arguments but 3 were given

Reason: parameter passing problem. Note: self represents the created class instance itself, so various properties can be bound to self internally. When creating an instance, you cannot pass in empty parameters, you must pass in parameters that match the method. But note that self does not need to be passed, the Python interpreter will pass in the instance variables itself.

Error demonstration:

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

Correct demonstration:

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

or

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

Guess you like

Origin blog.csdn.net/LZL2020LZL/article/details/131689328