2014-09-12 67 views
1

我不知道它爲什麼不起作用;它返回此錯誤:TypeError:my_function()需要正好4個參數(給出5個)

TypeError: create_selected_properties_json_doc() takes exactly 4 arguments (5 given) 

代碼:

def another_function(): 

    confirmed_diagnostic_keys = [some, keys] 
    generalized_stage_keys = [some, keys] 
    stage_acording_to_uicc_keys = [some, keys] 

    self.create_selected_properties_json_doc(self, form, 'hallelluja', confirmed_diagnostic_keys) 
    self.create_selected_properties_json_doc(self, form, 'mashalla', generalized_stage_keys) 
    self.create_selected_properties_json_doc(self, form, "heheeyy", stage_acording_to_uicc_keys) 


def create_selected_properties_json_doc(self, form, json_obj_key, form_element_ids): 

     # Create an empty dictionary 
     json_obj[json_obj_key] = {} 

     #Loop through the keys in list 
     for key in form_element_ids: 

      #check if the keys matches any of form's key 
      if key in form: 

       #Add values of form elements to the dictionary properties 
       json_obj[json_obj_key][key] = form[key] 
      else: 
       # Otherwise add "Jo" to the respective dictionary property 
       json_obj[json_obj_key][key] = 'NO' 

     return json_obj 
+0

'self'隱式傳遞,您不需要手動執行 - 您實際上正在調用'Class.method(self,self,...)'。 – jonrsharpe 2014-09-12 10:32:38

回答

4

不通過self作爲第一個參數:

self.create_selected_properties_json_doc(form, 'hallelluja', confirmed_diagnostic_keys) 

self.create_selected_properties_json_docbound method。當被調用時,self會自動作爲第一個參數傳遞給create_selected_properties_json_doc,所以您應該只傳遞其餘的參數。


爲了避免NameError: global name 'json_obj' is not defined,您需要定義json_objcreate_selected_properties_json_doc

def create_selected_properties_json_doc(self, form, json_obj_key, form_element_ids): 
    json_obj = dict() 
    # Create an empty dictionary 
    json_obj[json_obj_key] = {} 

存儲由create_selected_properties_json_docanother_function返回的字典,它分配給一個變量:

def another_function(): 

    confirmed_diagnostic_keys = [some, keys] 
    generalized_stage_keys = [some, keys] 
    stage_acording_to_uicc_keys = [some, keys] 

    json1 = self.create_selected_properties_json_doc(
     form, 'hallelluja', confirmed_diagnostic_keys) 
    json2 = self.create_selected_properties_json_doc(
     form, 'mashalla', generalized_stage_keys) 
    json2 = self.create_selected_properties_json_doc(
     form, "heheeyy", stage_acording_to_uicc_keys) 
+0

我必須在函數another_function()返回什麼值,因爲如果我返回json_obj它顯示我的錯誤:NameError:全局名稱'json_obj'沒有定義,因爲自我參數我沒有paas在函數create_selected_properties_json_doc() – KosovoDev 2014-09-12 12:35:12

+0

什麼是'json_obj'?它沒有在您發佈的任何代碼中定義。 – unutbu 2014-09-12 12:45:03

+0

json_obj是函數create_selected_properties_json_doc()的返回值,這意味着它是一個json對象。所以我不知道如何在another_function()返回json_obj – KosovoDev 2014-09-12 12:49:00

1

當傳遞參數給一個函數時避免傳遞自我(這是對象引用並自動傳遞自我s隱式傳遞,你不需要手動完成。

例如:它只是爲了讓你明白。

def binary_search(self,input_array,left_index,right_index): 
    mid=left_index+(right_index-left_index)/2 
    return mid 
def call_function(self,*arg): 
    self.binary_search(input_array,left_index,right_index) #self is not 
                  passed 
相關問題