December 23rd, 2012, 01:43 AM
I think it is like functions as parameters? I'm very new to python.
how would i correct this... want to allow the user of this function to cast the string to to the correct type by entering the desired type as an argument (so i cant later validate against that type within the function)
def get(message = "a value " cast_func = int):
print "Please enter " + message
December 23rd, 2012, 06:04 AM
I wonder, whether what you try to do could be useful, as there are already built-in functions (int(), float(); long(), str(), ...) available for converting to appropriate types
Even if you manage to write such a function, as I understand, each time based on the context in which the function is being used, it is up to you to provide manually, the corresponding casting function value as the second argument of your defined function. So I don't see any generalization that help avoiding rewriting the same code multiple times.
December 23rd, 2012, 10:38 AM
Your function as written is fine except it needs a comma between the parameters.