for some reason, strings appear to be passed in as a reference into a Python lambda. For this reason, only the last values of type
and definition
are being used in the lambda functions. The output should be:
>>> value (a,b) stuff
>>> time Some stuff
>>> did do otherStuff
but instead, I'm getting this as output:
>>> did do otherStuff
>>> did do otherStuff
>>> did do otherStuff
Any help with understating what is going on would be much much appreciated.
# A definition is the text following space after the type name.
# Get a value from the user.
# A value is a int from a to b inclusive.
# Signature in config: value (a,b) name
# str -> int
def get_value(definition):
print("value " + definition)
# Get a time from the user.
# A time is a float.
# Signature in config: time name
# str -> float
def get_time(definition):
print("time " + definition)
# Gets a did? from the user.
# A did_do is a bool.
# Signature in config: did_do name
# str -> bool
def get_did_do(definition):
print("did do " + definition)
# Takes in the data for a config and returns a list of functions to call the meet it's criteria.
# list of str -> list of (none -> str)
def config_to_functions(config):
type_to_input_functions = get_type_map()
functions = []
for line in config:
line = line.split(" ", 1)
type = line[0]
definition = line[1]
functions.append(lambda: type_to_input_functions[type](definition))
return functions
# Returns a map of type names to input functions.
# An input function is a fuc(str) -> str.
# none -> map of str to (str -> str)
def get_type_map():
return {
"value" : get_value,
"time" : get_time,
"did_do" : get_did_do,
}
# Runs the program.
def main():
mockConfig = ["value (a,b) stuff", "time Some stuff", "did_do otherStuff"]
input_functions = config_to_functions(mockConfig)
for func in input_functions:
func()
main()