Python错误:TypeError: 'int' object is not callable解决办法

版权声明:如需转载,请与我联系。WX:reborn0502 https://blog.csdn.net/gaifuxi9518/article/details/81193296

今天在练习Python类相关的知识时遇到了一个TypeError,也就是类型错误。该错误的意思是Int型的对象是不可调用的(not callable)。

class User():
	def __init__(self,name,age,number):
		self.name = name
		self.age = age
		self.custom = number

	def custom(self):
		print('the number of custom is '+str(self.custom))

u = User('reborn',23,40)
u.custom()
D:\>python test.py
Traceback (most recent call last):
  File "test.py", line 11, in <module>
    u.custom()
TypeError: 'int' object is not callable

看到这个错误我先是一愣,心想:“int对象不可调用?我没有调用Int型数据啊,我调用的是一个函数方法!”。调来调去都没有解决。Google后才发现,这个错误之所以发生,是因为我变量名和函数名写重复了!都用的custom。

当这两个名称重复时,程序会默认调用Int型对象,但Int对象没有什么调用可言,就爆出了这个错误,解决方法也很简单,要么更改变量名,要么更改方法名。

猜你喜欢

转载自blog.csdn.net/gaifuxi9518/article/details/81193296
今日推荐