special_func पुन: छोड़ने की कोशिश करने से बचने के लिए:
def special_func(test_case_dict):
final_dict = {}
exception_dict = {}
def try_except_avoider(test_case_dict):
try:
for k,v in test_case_dict.items():
final_dict[k]=eval(v) #If no exception evaluate the function and add it to final_dict
except Exception as e:
exception_dict[k]=e #extract exception
test_case_dict.pop(k)
try_except_avoider(test_case_dict) #recursive function to handle remaining functions
finally: #cleanup
final_dict.update(exception_dict)
return final_dict #combine exception dict and final dict
return try_except_avoider(test_case_dict)
भागो कोड:
def add(a,b):
return (a+b)
def sub(a,b):
return (a-b)
def mul(a,b):
return (a*b)
case = {"AddFunc":"add(8,8)","SubFunc":"sub(p,5)","MulFunc":"mul(9,6)"}
solution = special_func(case)
आउटपुट लगता है:
{'AddFunc': 16, 'MulFunc': 54, 'SubFunc': NameError("name 'p' is not defined")}
चर में बदलने के लिए:
:
locals().update(solution)
चर कैसा लगेगा 210
AddFunc = 16, MulFunc = 54, SubFunc = NameError("name 'p' is not defined")
आप बाधित होने पर एक 'try' ब्लॉक पर वापस नहीं आ सकते हैं, नहीं। –
मुझे नहीं लगता। कुछ संरचना को प्रवाह को विभाजित करना होता है, और इस स्निपेट में 'do_smth2') निष्पादित करने के लिए अगला बिंदु निर्दिष्ट करें। – Jokester
संभावित डुप्लिकेट [अपवादों पर "फिर से शुरू करें" के लिए एक पाइथोनिक तरीका?] (Http://stackoverflow.com/questions/18655481/a-pythonic-way-for-resume-next-on-exceptions) –