I have a bunch of functions that I am storing in a dictionary used to gather data from a more "cryptic" source (I have written functions to access this data).
In my code, I want to create "visibility" of what functions / parameters are loading variables used in the rest of the class. So, I would like to have a class where, upon init, a dictionary of functions stands up that can be used by further functions in the class. The issue I am running into is that I want these functions to be called only when they are retrieved from the dictionary by a later function. I do not want the functions evaluated upon init.
Problem: Some of the functions I am passing into the dictionary are "incomplete" as I would like to pass in additional parameters allowed via partial. The issue is that it appears init of the class evaluates all the functions in the dictionary rather than storing them as functions. I get an error from partial telling me that the first argument must be callable.
Here is an example of what I am doing (age works, month does not):
from functools import partial as part
class A:
def __init__(self):
self.rawInput={
'age':lu.source('personalInfo', 'age', asArray=1)
,'month':lu.source('employInfo', 'months_employed')
}
self.outputDict={}
self.resultsDict={}
class output(object):
def age(self):
age = A().rawInput['age']
return len(age)
def month(self):
stuff=[]
for x in range(0,1):
month = part(A().rawInput['month'], x=x)
stuff.append(month)
return stuff
SOLUTION
Ah, looks like the posted summary from 7stud works. I just now just place the values / functions into the dict as partials with standard parameters and then pass additional ones as needed in the function call
from functools import partial as part
def source(name, attrib, none=None):
if none!=None:
print 'ham'
else:
print 'eggs'
class A:
def __init__(self):
self.rawInput={
'age':part(source,'personalInfo', 'age')
,'month':part(source,'employInfo', 'months_employed')
}
self.outputDict={}
self.resultsDict={}
class output:
def age(self):
A().rawInput['age']()
def month(self):
x = 1
A().rawInput['month'](x)
c = A.output()
c.age()
c.month()
eggs
ham