TypeError:必须使用Interface实例作为第一个参数调用未绑定方法getInterfaceName()(没有任何内容)

时间:2017-07-18 19:04:49

标签: python typeerror

班级单位:     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)

类接口:     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)

1 个答案:

答案 0 :(得分:1)

您没有向我们显示导致错误的getInterfaceName()号召唤,这使您更难以帮助您。

但是,我猜这个电话看起来像这样:

something = Interface.getInterfaceName()

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

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