简体   繁体   中英

Python - call function within itself (same parameters)

I am calling the same function within itself.

base_url="www.myurl.com"
urls_1="www.myurl.com/1"
urls_2="www.myurl.com/2"
rep_1="/report1"
rep_2="/report2"

    def get_response(url, report):
      response=requests.get(url,report, headers=header)
      data=response.json()

      if (len(data))==100:
          header.update({"range":rep + str(range_from + 100) + "-" + 
          str(range_to + 100)})
          **data2=get_response(?,?)**

     else:
         return data

When I call the function inside, I want to use the same parameters that I used to call the outer function.(Only writing the function once)

This outer function gets called hundreds of times with all different parameters; sometimes it gets called within a for loop.

Thanks in advance.

What you're attempting is known as recursion in computer science.

def my_function(x,y,z): 
    something = my_function(x, y, z)

However, this is going to throw you into an infinite loop. For recursion to work, you have to have a base case -- a way of stopping -- and then simplify the problem at each step. See on-line tutorials and examples of recursion.

data2 = get_response(URL,报告)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM