python - 类型错误:必须使用 Interface 实例作为第一个参数来调用未绑定(bind)方法 getInterfaceName()(什么也没有)

标签 python typeerror

类(class)单位: def init(self, _chassisId, _unitNo, _interface): self.chassisId = _chassisId self.unitNo = _unitNo self.interface = _interface

def getInterface(self):
    return self.interface

@staticmethod
def parse(elem):
    unitList = elem.find(UNIT+LIST)
    chassisList = []
    for unit in unitList.findall(UNIT):
        try:
            unitNumber = unit.find(UNIT_NUMBER).text
            interface = unit.find(INTERFACE)
            interface = ""
            chassisIdElem = unit.find(CHASSIS_ID)
            chassisId = ""
            if chassisIdElem is not None:
                chassisId = unit.find(CHASSIS_ID).text
            elif unit.find(BURNED_IN_MAC) is not None:
                chassisId = unit.find(BURNED_IN_MAC).text
            chassisId = chassisId.replace(".", "").replace(":", "").upper()
            chassis = Unit(chassisId, interface, unitNumber)
            chassisList.append(chassis)
        except Exception as e:
            print "Unit details not found", e
        return chassisList

def getChassisId(self):
    return self.chassisId

def __str__(self):
    str = "\n"
    str += "\nUnit Details:- "
    len = str.__len__();
    str += "\n"
    for i in range(1,len-1):
        str += "-"
    str += "\nUnit: " + self.unitNo
    str += "\nChassis Id: " + self.chassisId
    str += "\nInterfaces: " + self.interfaces
    return str

def __add__(self, other):
    return str(self) + other

def __radd__(self, other):
    return other + str(self)

类接口(interface): def init(self, _linkState, _interfaceName): self.linkState = _linkState self.interfaceName = _interfaceName

@staticmethod
def parse(elem):
    prefix = Device.getPrefix(elem.tag)
    interfaceList = elem.find(INTERFACE + LIST)
    interfaceNameTag = eval(prefix + "_INTERFACE_NAME")
    linkStateTag = eval(prefix + "_LINK_STATE")
    interfaces = []
    for interface in interfaceList.findall(INTERFACE):
        try:
            interfaceName = interface.find(interfaceNameTag).text
            linkStateElem = interface.find(LINK_STATE)
            linkState = ""
            if linkStateElem is not None:
                linkState = interface.find(LINK_STATE).text
            elif interface.find(LINE_PROTOCOL) is not None:
                linkState = interface.find(LINE_PROTOCOL).text
            interface = Interface(linkState, Name)
            interfaces.append(interface)
        except Exception as e:
            print "Interface details not found", e
            return interfaces

def getLinkState(self):
    return self.linkState

def getInterfaceName(self):
    return self.interfaceName

def __str__(self):
    str = "\n"
    str += "\nInterface  Details:- "
    len = str.__len__();
    str += "\n"
    for i in range(1,len-1):
        str += "-"
    str += "\nLink State: " + self.linkState
    str += "\nInterface Name: " + self.interfaceName
    return str

def __add__(self, other):
    return str(self) + other

def __radd__(self, other):
    return other + str(self)

最佳答案

您没有向我们展示导致错误的 getInterfaceName() 调用,这使得我们更难为您提供帮助。

但是,我猜调用看起来像这样:

something = Interface.getInterfaceName()

你不能那样做。您必须创建 Interface实例,然后调用其 .getInterfaceName() 方法:

myInterface = Interface()
something = myInterface.getInterfaceName()

关于python - 类型错误:必须使用 Interface 实例作为第一个参数来调用未绑定(bind)方法 getInterfaceName()(什么也没有),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45175113/

相关文章:

python - Python 中的 dict() 问题,TypeError :'tuple' 对象不可调用

python - 从 JSON 文件访问特定值时出错

python - 如何计算一列的累计和并新建一列?

python - OpenCV:Canny 边缘检测器获得 minEnclosingCircle

python - App Engine session 超时

ssl - 从 Ruby 1.9.3 升级到 2.0.0 期间出现 Mysql2 错误

javascript - 使用 ReactJS 通过 Prop 将函数传递给子组件

python - TypeError: &: 'unicode' 和 'unicode' 不支持的操作数类型

python - 用两个不同的分隔符拆分字符串,将两个单词大写,只包含分隔符,然后再添加两个分隔符的最佳方法是什么?

python - 如何检查字符串是否包含子字符串的所有字符?