Home > Mobile >  Combining multiple functions into one
Combining multiple functions into one

Time:12-10

How to combine the following functions into one using a "type" variable? As you can see the functions below are very similar. I would like to pass an additional variable into the function, like type=input, and have the first function be the output.

    def getInputID(self, obj):
    tempID = self.inputIDs.pop(0)
    self.inputNodes[tempID] = obj
    self.objects[tempID] = obj
    return tempID

def getOutputID(self, obj):
    tempID = self.outputIDs.pop(0)
    self.outputNodes[tempID] = obj
    self.objects[tempID] = obj
    return tempID

def getOperatorID(self, obj):
    tempID = self.operatorIDs.pop(0)
    self.operatorNodes[tempID] = obj
    self.objects[tempID] = obj
    return tempID

CodePudding user response:

You have three lists, stored as separate variables.

Imagine you instead had three lists in a single dict:

    def __init__(self):
        self.ids = dict(input=[], output=[], operator=[])

Then you could pass in e.g. type='input' and use self.ids[type] to manipulate the appropriate list.


Or you could rely on the getattr / setattr builtins, essentially using self as that dict.


Or you could define a @staticmethod helper that accepts self.inputIDs or one of the other two, and manipulates it.

CodePudding user response:

You can define one method

def getID(self, obj, ids, nodes):
    tempID = self.ids.pop(0)
    nodes[tempID] = obj
    self.objects[tempID] = obj
    return tempID

and then simplify the others:

def getInputID(self, obj):
    return getID(obj, self.inputIDs, self.inputNodes)

def getOutputID(self, obj):
    return getID(obj, self.outputIDs, self.outputNodes)

def getOperatorID(self, obj):
    return getID(obj, self.operatorIDs, self.operatorNodes)

But, likely, you shouldn't have those variables in the first place. It looks like inputIDs and inputNodes always belong together. Probably you never want to mix different IDs with different nodes, which is currently possible in your design.

So perhaps you want to have a small class that binds IDs and nodes together. (So my suggestion is exactly opposite to that of @J_H, who proposes to combine all IDs and combine all nodes.)

class NodeRegistrar:
    def __init__(self):
        self.ids = []  # initialize your ids here
        self.nodes = []
        
    def register(self, obj):
        id = self.ids.pop(0)
        self.nodes[id] = obj
        return id

and then use it

def getID(self, obj, registrar):
    tempID = registrar.register(obj)
    self.objects[tempID] = obj
    return tempID

def getInputID(self, obj):
    return getID(obj, self.inputRegistrar)

def getOutputID(self, obj):
    return getID(obj, self.outputRegistrar)

def getOperatorID(self, obj):
    return getID(obj, self.operatorRegistrar)

You could then still have a dictionary of those and use the registrars by name. But be aware that putting strings into a dictionary may make your app vulnerable to typos.

  • Related