Python Type Dispatching with variables, is it possible?

89 views Asked by At

I am developing a script that parses through a large number of IDs and want to build an abstraction that can hold all of these IDs. I also want this object to be able to be easily modified to add new IDs.

This is what I have so far:

class action: # abstraction for all IDs in an action
def __init__(self, dID=".", cID='.', dTp='.', dVs='.', mcID='.', aID='.', lID='.', pID='.', uID='.', uSe='.', udTp='.', componentID='.', eCx='.', eUr='.', eTp='.', rUrl='.', sec='.', oID='.', oVa='.', oCu='.', sID='.', saID='.', socNetworkUserID='.'):
    self._dID = dID
    self._cID = cID
    self._dTp = dTp
    self._dVs = dVs
    self._mcID = mcID
    self._aID = aID
    self._lID = lID
    self._pID = pID
    self._uID = uID #Display and Insights
    self._uSe = uSe
    self._udTp = udTp
    self._componentID = componentID
    self._eCx = eCx #Display and Insights
    self._eUr = eUr
    self._eTp = eTp #Display and Insights
    self._rUrl = rUrl
    self._sec = sec
    self._oID = oID #Display and Insights
    self._oVa = oVa
    self._oCu = oCu
    self._sID  = sID
    self._saID = saID
    self._socNetworkUserID = socNetworkUserID
    self._empty_params = []

def insert_id_val(self, name, value): #Utility method
    return

def insert_id_val_display(self, name, value):
    item_map = {"dID" : _dID, "cID" : _cID, "dTp" : _dTp, "dVs" : _dVs, 
                "mcID" : _mcID, "aID" : _aID, "lID" : _lID, "pID" : _pID,
                "uID" : _uID, "uSe" : _uSe, "udTp" : _udTp, "componentID" : _componentID,
                "eCx" : _eCx,"eUr" : _eUr, "eTp" : _eTp, "rUrl" : _rUrl,
                "sec" : _sec, "oID" : _oID, "oVa" : _oVa, "oCu" : _oCu,
                "socNetworkUserID" : _socNetworkUserID, "_" : _empty_params}
    self.item_map[name] = value

def insert_id_val_insights(self, name, value):
    item_map = {"eTp" : self._eTp, "eCx" : self._eCx, "uID" : self._uID, "oID" : self._oID,
                "sID" : self._sID, "saID" : self._saID}
    item_map[name] = value

I tried two ways of doing type dispatching with two different ways, in the two different insert functions, but they both error. I have seen examples where people do things like Type-Dispatching Example but they don't show how to use type dispatching with setting, only getting.

My implementation gives:

global name '_dID' is not defined at line 193 of program.py

How does one use type dispatching with setting variables? Is it even possible? If not, how else can I quickly make an abstraction like the one above without having to type a bunch of if statements?

EDIT: To add, I cannot instantiate the object with all of the IDs as their values are not known at instantiation.

1

There are 1 answers

3
Daniel Roseman On

I'm really not sure what you're asking here, or what exactly you mean by type dispatching. If you're asking about how to dynamically set attributes in a class, you can easily do that with **kwargs:

def __init__(self, **kwargs):
    self.__dict__.update(kwargs)

and now you can instantiate it with action(dID='foo', oID='bar') and refer internally to self.dID and self.oID.