千鋒教育-做有情懷、有良心、有品質的職業(yè)教育機構

手機站
千鋒教育

千鋒學習站 | 隨時隨地免費學

千鋒教育

掃一掃進入千鋒手機站

領取全套視頻
千鋒教育

關注千鋒學習站小程序
隨時隨地免費學習課程

當前位置:首頁  >  技術干貨  > Python函數(shù)能夠訪問哪些變量名稱

Python函數(shù)能夠訪問哪些變量名稱

來源:千鋒教育
發(fā)布人:xqq
時間: 2023-07-21 16:27:00 1689928020

  一、全局變量

  全局變量是在函數(shù)外部定義的變量,可以在整個程序的任何位置訪問。在函數(shù)內部,可以使用global關鍵字聲明全局變量,在函數(shù)內部修改全局變量的值。

  global_variable = "This is a global variable."

  def access_global_variable():

  print(global_variable)

  def modify_global_variable():

  global global_variable

  global_variable = "Modified global variable."

  access_global_variable() # 輸出 "This is a global variable."

  modify_global_variable()

  access_global_variable() # 輸出 "Modified global variable."

  二、局部變量

  局部變量是在函數(shù)內部定義的變量,其只在函數(shù)內部可用,如果函數(shù)外部想要訪問,需要將其作為函數(shù)返回值返回。在函數(shù)內部想要修改局部變量的值,需要使用global關鍵字聲明。

  def access_local_variable():

  local_variable = "This is a local variable."

  print(local_variable)

  def modify_local_variable():

  local_variable = "This is a local variable."

  print("Before modification: ", local_variable)

  local_variable = "Modified local variable."

  print("After modification: ", local_variable)

  access_local_variable() # 輸出 "This is a local variable."

  modify_local_variable()

  # 輸出

  # Before modification: This is a local variable.

  # After modification: Modified local variable.

  三、參數(shù)

  函數(shù)的參數(shù)是在函數(shù)定義時指定的,可以在函數(shù)內部使用,如果沒有使用global聲明,其僅在函數(shù)內部可用。

  def access_parameter(parameter):

  print(parameter)

  def modify_parameter(parameter):

  print("Before modification: ", parameter)

  parameter = "Modified parameter."

  print("After modification: ", parameter)

  access_parameter("This is a parameter.")

  # 輸出 "This is a parameter."

  modify_parameter("This is a parameter.")

  # 輸出

  # Before modification: This is a parameter.

  # After modification: Modified parameter.

  四、嵌套函數(shù)中的變量名稱

  如果在一個函數(shù)內部再定義一個函數(shù),那么嵌套函數(shù)可以訪問其外部函數(shù)中定義的變量。在嵌套函數(shù)內部,如果修改外部函數(shù)的變量,需要使用nonlocal關鍵字聲明。

  def outer_function():

  outer_variable = "This is an outer variable."

  def inner_function():

  print("Inner function: ", outer_variable)

  inner_function()

  outer_function() # 輸出 "Inner function: This is an outer variable."

  def modify_outer_variable():

  outer_variable = "This is an outer variable."

  def inner_function():

  nonlocal outer_variable

  outer_variable = "Modified outer variable."

  print("Inner function: ", outer_variable)

  inner_function()

  print("Outer function: ", outer_variable)

  modify_outer_variable()

  # 輸出

  # Inner function: Modified outer variable.

  # Outer function: Modified outer variable.

  五、總結

  Python函數(shù)在其定義和調用期間可以訪問全局變量、局部變量、參數(shù)以及嵌套函數(shù)中的變量名稱。在訪問和修改變量的時候,需要注意使用global、nonlocal關鍵字進行聲明。

tags: python教程
聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。
10年以上業(yè)內強師集結,手把手帶你蛻變精英
請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
免費領取
今日已有369人領取成功
劉同學 138****2860 剛剛成功領取
王同學 131****2015 剛剛成功領取
張同學 133****4652 剛剛成功領取
李同學 135****8607 剛剛成功領取
楊同學 132****5667 剛剛成功領取
岳同學 134****6652 剛剛成功領取
梁同學 157****2950 剛剛成功領取
劉同學 189****1015 剛剛成功領取
張同學 155****4678 剛剛成功領取
鄒同學 139****2907 剛剛成功領取
董同學 138****2867 剛剛成功領取
周同學 136****3602 剛剛成功領取
相關推薦HOT