This is another one of my pointless endeavors threads. Meaning that whether or not I figure this out has no immediate benefit for the actual functionality of my code. Also, its not CivIV specific so I might as well post in somewhere else entirely... But bare with me. And if you think I'm (still) being silly you can just skip this one. Because I already know its a futile quest.
The background is that I recently figured out a way to get rid of the unaesthetic assignment lines for counter variables and empty data structures. Because I can just add those on the definition line, like this:
As a bonus the actual function becomes more versatile, as it would be possible to for example use it to add to a pre-existing list by entering a third argument.
Now the next step in cleaning up my code is to get rid of the pesky default value assignment in some __init__ definitions. Example:
Clearly there must be another way to set all these class fields to a None value without typing 7 individual statement lines?
I believe that my own ideas thus far have revolved around putting all the attribute names in list and looping it in order to assign each field the same default value. But there were technical challenges I really couldn't overcome. It would however look something like this:
And the setDefaultValue() function would look something like this:
I guess that I'm still struggling wit the eval() built-in function...
Also I've though of tuple assignment, but then I need a data structure that holds the exact number of None values. But maybe I could make a helper function that makes that for me?
Then I could have:
But I'm not sure this is an improvement. Could I get rid of the self. bits if I make these class variables instead? Like:
Does anyone have a better implementation for this? (And I still realize this is pretty pointless, but this is actually how I learn. By figuring out some seemingly meaningless idea like this allows me to do something pretty awesome in another context. The thing is of course that I won't know in beforehand when, where or even if it will pay off.)
The background is that I recently figured out a way to get rid of the unaesthetic assignment lines for counter variables and empty data structures. Because I can just add those on the definition line, like this:
Code:
def randomFunction(arg1, arg2, lEmptyList=[], iCounter=0):
Now the next step in cleaning up my code is to get rid of the pesky default value assignment in some __init__ definitions. Example:
Code:
def __init__(self):
self.player = None
self.plotList = None
self.coords = None
self.Key = None
self.CyPlayer = None
self.CyPlot = None
self.CyCity = None
I believe that my own ideas thus far have revolved around putting all the attribute names in list and looping it in order to assign each field the same default value. But there were technical challenges I really couldn't overcome. It would however look something like this:
Code:
def __init__(self):
setDefaultValue(self, None, "player", "plotList", "coords", "Key", "CyPlayer", "CyPlot", "CyCity")
Code:
def setDefaultValue(pInstance, value, *args):
for variable in args:
eval("pInstance." + variable + "=value")
Also I've though of tuple assignment, but then I need a data structure that holds the exact number of None values. But maybe I could make a helper function that makes that for me?
Code:
def getNoneList(iNumEntries, lNoneList=[]):
while iNumEntries > 0:
lNoneList.append(None)
iNumEntries -= 1
return lNoneList
Code:
def __init__(self):
self.player, self.plotList, self.coords, self.Key, self.CyPlayer, self.CyPlot, self.CyCity = getNoneList(7)
Code:
class Context:
player, plotList, coords, Key, CyPlayer, CyPlot, CyCity = getNoneList(7)
def __init__(self):
pass