Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 类型错误:';非类型';对象在第二次引用时不可调用_Python_Oop - Fatal编程技术网

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.

我完全擅长编写代码,更不用说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.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)

完美的没问题!