python - Multiple inheritance with different argument size -
i know if possible... currently, have single inheritance... , it's working fine...
in ciscoplatform class, not have init method it's expecting 6 arguments when create object (as i'm using baseplatform init method)...
i create object , works fine:
ntw_device = [] device = ciscoplatform(list[0],list[1],list[2],list[3],list[4],list[5]) ntw_device.append(device) class baseplatform(object): def __init__(self,ip,hostname,username,password,vendor,type): self.ip = ip self.hostname = hostname self.username = username self.password = password self.vendor = vendor self.type = type class cisco(baseplatform,interface): pass
i introduce new base class called interface
class interface(object): def __init__(self,host,interface,vlan): self.host = host self.interface = interface self.vlan = vlan
how able inherit both parent classes different number of arguments? this? *assuming switchport = [] - list of objects
class ciscoplatform(baseplatform,interface): def __init__(self): baseclass.__init__(self,ntw_device[0].ip,ntw_device[1].hostname,ntw_device[2].username,ntw_device[3].password,ntw_device[4].vendor,ntw_device[5].type) interface.__init__(self,switchport[0].add dictionary[1].interface,switchport[2].vlan)
how still able create objects in fashion again ciscoplatform no longer accepts 6 arguments??
device = ciscoplatform(list[0],list[1],list[2],list[3],list[4],list[5])
i not sure if purpose ensure ciscoplatform
class can accept different numbers of arguments, if that's case, won't hard. when initialize new subclass, __init__()
called initiate ciscoplatform
. need check number of arguments passed in before decide base classes' __init__
should used.
class ciscoplatform(baseplatform,interface): def __init__(self, *arg): if len(arg) == 6: ip,hostname,username,password,vendor,type = arg
baseclass.__init__(self,ip,hostname,username,password,vendor,type ) elif len(arg) == 3: host,interface,vlan = arg interface.__init__(self,host,interface,vlan) else: raise valueerror("inconsistent arguments number")
Comments
Post a Comment