Is there a way to automate the defining of a function in python? -
i wondering if there way automate defining of function. instance want set of functions like:
def add1: list.append(x) def add2: list.append(y) def add3: list2.append(x) ...
the problem have define lots of these, 232 exact. there faster way this? also, use python 3.2.
you may use exec
example:
def your_common_function(i, txt): # here may decide arguments according "i" value if == 1: print(txt) else: print(txt+'s') in range(1, 233): exec("def add%d(txt):\n\tyour_common_function(%d, txt)" % (i, i)) add1("hello world") # prints "hello world" add167("hello world") # prints "hello worlds"
edit changed code allow define different behaviours according "function number"
Comments
Post a Comment