2011-08-15 10 views
1

で再帰ループから抜け出す:私は最後のelse節が有効であることがどのように変化するかは「予期せず終了」私はPythonで結果次のコードを、持っているPythonの

def profile_videos_sort_and_filter(profile, sort, filter, uncredited_videos, list_of_credits=[]): 
    """Given a sort and filter, this will order the credits and return a credit_set.""" 
    if filter == 'user' or filter == 'all': 
     if filter == 'user': 
      credit_set = profile.videocredit_set.filter(video__uploaded_by=profile) 
     if filter == 'all': 
      credit_set = profile.videocredit_set.all() 

     if sort == 'alphabetical': 
      credit_set = conform_videos_and_credits(credit_set, uncredited_videos, sort) 
     if sort == 'newest': 
      credit_set = conform_videos_and_credits(credit_set, uncredited_videos, sort) 
     if sort =='position': 
      credit_set = credit_set.order_by('position') 
      list_of_credits = position_credit_set(profile, credit_set, filter, uncredited_videos) 

    elif filter == 'others': 
     credit_set = profile.videocredit_set.exclude(video__uploaded_by=profile) 
     if sort == 'alphabetical': 
      credit_set = credit_set.order_by('video__title') 
     if sort == 'newest': 
      credit_set = credit_set.order_by('video__uploaded_at') 
     if sort == 'position': 
      credit_set = credit_set.order_by('position') 
      list_of_credits = position_credit_set(profile, credit_set, filter, uncredited_videos) 

    #### THIS IS THE PART THAT CAUSES THE ERROR ### 
    else: 
     ### if none of the above, call the function, passing default parameters ### 
     profile_videos_sort_and_filter(profile, uncredited_videos=uncredited_videos, 
             sort='position', filter='newest') 

    return credit_set, list_of_credits 

?ありがとうございました。

+0

補足すると、関数の引数のデフォルト値をリストに設定しないでください(例では、list_of_credits = [])。詳細はこちらを参照してください:http://effbot.org/zone/default-values.htm – Etienne

答えて

2

sort='position'および/またはfilter='newest'の条件を追加します。

1

filter=='newest'はどこで処理しますか? elif filter=='newest'を追加してそのようなことをしない限り、コードは永遠にループします。