Python: Function always returns None

前端 未结 4 762
迷失自我
迷失自我 2020-11-29 11:36

I have some Python code that basically looks like this:

my_start_list = ...

def process ( my_list ):
    #do some stuff

    if len(my_list) > 1:
                


        
4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 12:13

    As others have pointed out, you are missing a return statement.

    I personally would turn that tail recursion into iteration:

    def process(my_list):
        while True:
            # do some stuff
            if len(my_list) <= 1:
                return my_list
    

    I think this makes the intent a little clearer, and also avoids some pitfalls associated with tail recursion.

提交回复
热议问题