return-type

why a python function still returns "None" though i am explicitly returning a value?

why a python function still returns "None" though i am explicitly returning a value? Question: def binarySearch(nums,low,high,target): if low<=high: mid=(low+high)//2 if nums[mid]==target: return mid if nums[mid]<target: binarySearch(nums,mid+1,high,target) else: binarySearch(nums,low,mid-1,target) else: return -1 def search(nums, target): return binarySearch(nums,0,len(nums)-1,target) nums=[-1,0,3,5,9,12] target=9 print(search(nums,target)) console output Expected output of the above python code for binary search is ‘4’. But …

Total answers: 1

How to specify multiple return types using type-hints

How to specify multiple return types using type-hints Question: I have a function in python that can either return a bool or a list. Is there a way to specify the return types using type hints? For example, is this the correct way to do it? def foo(id) -> list or bool: … Asked By: …

Total answers: 4

Apply pandas function to column to create multiple new columns?

Apply pandas function to column to create multiple new columns? Question: How to do this in pandas: I have a function extract_text_features on a single text column, returning multiple output columns. Specifically, the function returns 6 values. The function works, however there doesn’t seem to be any proper return type (pandas DataFrame/ numpy array/ Python …

Total answers: 16

Why should functions always return the same type?

Why should functions always return the same type? Question: I read somewhere that functions should always return only one type so the following code is considered as bad code: def x(foo): if ‘bar’ in foo: return (foo, ‘bar’) return None I guess the better solution would be def x(foo): if ‘bar’ in foo: return (foo, …

Total answers: 7