python函數參數理解

來源:互聯網
上載者:User

標籤:append   局部對象   個數   指定   參數傳遞   儲存位置   color   判斷   不同   

1、位置參數

  函數調用時,參數賦值按照位置順序依次賦值。

  e.g. 

1 def function(x):2 3  return x * x4 5 print function(2)

    輸出結果: 4

1 def function(x, n):2 3   return x / n4 5 print function(4, 2)

    輸出結果: 2

2、預設參數

  在函數定義時,直接指定參數的值。

  e.g.

 1 def function(x, n = 2): 2  3     s = 1 4  5     while n > 0: 6  7         n = n -1 8  9         s = s * x10 11     return s12 13 print function(20)

    輸出結果:400

  注意:

    1、必選參數必須在前,預設參數在後。

    2、當函數有多個參數時,把變化大的放在前面。

      好處:降低調用函數的難度。

    3、當有多個預設參數時,可以按順序,也可使用參數名直接賦值。

      e.g.

 1 def enroll(name, gender, age = 6, city = ‘beijing‘) : 2  3   print name 4  5   print gender 6  7   print age 8  9   print city10 11 enroll(‘aaa‘, ‘F‘, 7)

        輸出內容:

          aaa

          F

          7

          beijing

1 enroll(‘bbb‘, ‘M‘, 9, city = ‘tianjin‘)

        輸出內容:

          bbb

          M

          9

          tianjin

    4、特別注意

      e.g.

1 def add_end(L = []):2 3   L.append(‘END‘)4 5   return L6 7 add_end([1,2,3,4,5])

        輸出內容:

          [1,2,3,4,5,‘END‘]

      當多次調用add_end()時

1 add_end()2 3 add_end()

        輸出內容:

          [‘END‘, ‘END‘]

          這時就要對輸入參數進行判斷了:

 1 def add_end(L = []): 2  3     if L is None: 4  5         L = [] 6  7     L.append(‘END‘) 8  9     return L10 11 add_end()12 13 add_end()

          輸出內容:

            [‘END‘]

    5、能設計不變的對象就設計成不變的對象。

3、可變參數

  傳入的參數個數是可變的。

4、關鍵字參數

5、命名關鍵字參數

6、參數組合

總結:

  參數傳遞過程中存在倆個規則:

    1、通過引用將參數複製到局部範圍的對象中,意味著被用來訪問的函數參數的變數與提供給函數的對象無關,以為存在一個複製問題,修改局部變數不會修改未經處理資料。

    2、可以在適當的位置修改可變對象。可變對象主要是list和dict,適當的位置主要是在修改局部對象不會改變list對象或者dict對象的ID,也就是儲存位置。

 1 #!/usr/bin/python 2  3 def modifier(number, string, list): 4     number = 5  5     string = ‘GOODBYE‘ 6     list = [1,2,3] 7     print ‘inside:‘, number, string, list 8  9 num = 1010 string = ‘goodbye‘11 list = [4,5,6]12 print ‘before:‘, num, string, list13 14 modifier(num, string, list)15 16 print ‘after:‘, num, string, list

      輸出內容:

        before: 10 goodbye [4, 5, 6]

        inside: 5 GOODBYE [1, 2, 3]

        after: 10 goodbye [4, 5, 6]

      從輸出結果來看,資料交換前後資料並沒有發生改變,雖然在函數局部地區傳遞進來的參數進行了相應的修改,但是仍然不能改變實參對象的內容。這和C語言十分相似。

      因為傳遞進來的參數在函數內部進行了修改,也就是變數指向了不同的儲存區(對象),這就相當於C語言中的實參與形參,只是對形參進行了修改,實參沒有改變。

      所以說明函數內部對參數重新賦了值,並不會改變實參對象。

 1 #!/usr/bin/python 2 def modifier(list, dict): 3     list[0] = 10 4     dict[‘a‘] = 10 5     print ‘inside: list = %s, dict = %s‘ %(list, dict) 6  7 list = [1,2,3,4,5] 8 dict = {‘a‘ : 1, ‘b‘ : 2, ‘c‘ : 3} 9 print ‘defore: list = %s, dict = %s‘ %(list, dict) 10 11 modifier(list, dict)12 print ‘after: list = %s, dict = %s‘ %(list, dict)

        輸出結果:

          defore: list = [1, 2, 3, 4, 5], dict = {‘a‘: 1, ‘c‘: 3, ‘b‘: 2}
          inside:, list = [10, 2, 3, 4, 5], dict = {‘a‘: 10, ‘c‘: 3, ‘b‘: 2}
          after: list = [10, 2, 3, 4, 5], dict = {‘a‘: 10, ‘c‘: 3, ‘b‘: 2}

        從輸出結果可以看出,函數內部對傳遞進來的參數進行了修改,並且對實參對象也進行了修改,實際修改的是list和dict儲存位置上的資料,變數指向並沒有改變,所以實參對象也發生了變化。

 

python函數參數理解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.