Python 类型错误:';非类型';对象在第二次引用时不可调用
我完全擅长编写代码,更不用说python了。我用下面的代码面对这个错误。 文件名为Python 类型错误:';非类型';对象在第二次引用时不可调用,python,oop,Python,Oop,我完全擅长编写代码,更不用说python了。我用下面的代码面对这个错误。 文件名为abcd.py class device(): def login(self,ip): self.tn= telnetlib.Telnet(ip) self.tn.read_until("login: ") self.tn.write(login) def sendcommand(self,command): self.sendcommand= self.
abcd.py
class device():
def login(self,ip):
self.tn= telnetlib.Telnet(ip)
self.tn.read_until("login: ")
self.tn.write(login)
def sendcommand(self,command):
self.sendcommand= self.tn.write(command)
此python代码由另一个文件导入
from abcd import *
def foo():
ip = 'ip address'
dev1 = switch()
dev1.login(ip)
dev1.sendcommand('cmd1')
dev1.sendcommand('cmd2')
foo()
当我调用foo函数时,所有操作都会正确执行,直到到达dev1.sendcommand('cmd2')
。收到的错误是
dev1.sendcommand('cmd2')
TypeError: 'NoneType' object is not callable
我根本不知道为什么会这样。我是否以某种方式修改了对象?是。当您执行
self.sendcommand=self.tn.write(command)
时,您将使用self.tn.write(command)
的值覆盖方法sendcommand
。为变量使用与方法不同的名称。问题似乎就在这一行-
def sendcommand(self,command):
self.sendcommand= self.tn.write(command)
您正在将writer()的返回值设置为self.sendcommand,这将覆盖函数,您不应该这样做,只需调用函数,而不在任何地方设置返回值。范例-
def sendcommand(self,command):
self.tn.write(command)
完美的没问题!