简体   繁体   中英

python: function that will call itself

i was wondering if i can get your help with the stucture.logic of a function that will need to call itself

def populate_frequency5(d,data,total_compare):
  freq=[]
  prev = None
  for row in d:
    if prev is None or prev==row[11]:
      freq.append(row[16])
      doctor=row[10]
      drug=row[11][:row[11].find(' ')].capitalize()
    else:
      make_image_url_doctor(freq,doctor,drug,data)
      total_compare=True
      del freq[:]
    prev=row[11]  
  if total_compare:
    (b1,bla,bla1)=medications_subset2(data,[drug.upper()])
    data1=calculate_creat_conc4(b1)
    populate_frequency5(data1,['a'],total_compare=True)
    total_compare=False
  1. the first time the function is called i need it to run this:

     def populate_frequency5(d,data,total_compare): freq=[] prev = None for row in d: if prev is None or prev==row[11]: freq.append(row[16]) doctor=row[10] drug=row[11][:row[11].find(' ')].capitalize() else: make_image_url_doctor(freq,doctor,drug,data) (b1,bla,bla1)=medications_subset2(data,[drug.upper()]) data1=calculate_creat_conc4(b1) del freq[:] prev=row[11] 
  2. then somehow the second time when i call it, i need it to run this way:

     def populate_frequency5(d,data,total_compare): freq=[] prev = None for row in d: if prev is None or prev==row[11]: freq.append(row[16]) doctor=row[10] drug=row[11][:row[11].find(' ')].capitalize() run_another_function() 

Your current logic is faulty and will lead to runaway recursion. If you ever make a recursive call, you'll pass it a total_compare of True ; but then within that recursive call it will not be set to False again, so when checked it will be true and yet another recursive call (with the same defect) will inevitably result.

The setting of total_compare in the calling instance (were it ever to execute, which it won't because of the runaway recursion) is irrelevant: it's the last statement ever executed there and it's setting a local variable, so of course it can be removed without any observable effects. Maybe you don't realize that each instance of a function in recursive calls has its own set of local variables (including arguments -- they're also local variables, just ones that get initializer by the caller), which is why I'm pointing this out explicitly.

Your examples "desired code" 1 and 2 don't really help because they never show the function calling itself. Under what conditions, exactly, does the function need to call itself recursively (a) if it was called non-recursively, (b) if it was already called recursively? Remember that, to avoid runaway recursion, there must be eventually reached a "base case" where no further recursion occurs.

Given that in your first ("runaway recursion") example the recursive call appears to be the last action of the caller (net of the useless and therefore removable setting of the total_compare local variable), I should also point out that such "tail recursion" can easily be turned into iteration (and, in Python, that's usually advisable, since Python does not optimize tail recursion as other languages do).

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