python - One vs many return values on dynamic method calls? -
python - One vs many return values on dynamic method calls? -
i'm accessing methods name using string :
def blah(str) : method = getattr(self,str) x1, x2 = method() the problem method i'm using returns 1 argument instead of 2 , next error :
typeerror: 'numpy.float64' object not iterable what cleanest way handle single vs. multiple homecoming values. thanks
maybe this:
result = method() try: x1, x2 = list(result) except typeerror: # "result" not iterable x1, x2 = [result, none] in python, it's easier inquire forgiveness permission
note doesn't cover cases method returns list wrong number of elements (i.e. not two).
for cases, might appropriate:
result = method() try: result_list = list(result) except typeerror: # "result" not iterable result_list = [result] number_of_vars = 2 final_list = [none] * number_of_vars final_list[:min(number_of_vars, len(result_list))] = result_list[:number_of_vars] x1, x2 = final_list python dynamic methods call
Comments
Post a Comment