Why should functions always return the same type?

前端 未结 7 2031
野性不改
野性不改 2020-12-01 03:31

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          


        
7条回答
  •  再見小時候
    2020-12-01 04:14

    I personally think it is perfectly fine for a function to return a tuple or None. However, a function should return at most 2 different types and the second one should be a None. A function should never return a string and list for example.

提交回复
热议问题