加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 钦州站长网 (https://www.0777zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Python干货:命名空间和作用域的基础知识整合

发布时间:2022-12-16 11:12:38 所属栏目:PHP教程 来源:
导读:  什么是命名空间?

  命名空间是一个系统,它对Python中的每个对象都有一个唯一的名称。对象可能是变量或方法。

  Python本身以Python字典的形式维护名称空间。

  在类似的行中,Python解释器可
  什么是命名空间?
 
  命名空间是一个系统,它对Python中的每个对象都有一个唯一的名称。对象可能是变量或方法。
 
  Python本身以Python字典的形式维护名称空间。
 
  在类似的行中,Python解释器可以理解代码中的确切方法或变量,这取决于名称空间。
 
  名称可能是任何Python方法或变量,空间取决于试图访问变量或方法的位置。
 
  命名空间的类型:
 
  当Python解释器只在没有和用户定义的模块、方法、类等情况下运行时,在名称空间中构建(如print()、id())这些函数。当用户创建一个模块时,将创建一个全局命名空间。
 
  内置命名空间包含全局命名空间;全局命名空间包含局部命名空间.

  命名空间的生存期:
 
  命名空间的生存期取决于对象的作用域,不可能从外部命名空间访问内部命名空间的对象。
 
  例子:
 
  # var1 is in the global namespace
 
  var1 = 5
 
  def some_func():
 
      # var2 is in the local namespace
 
      var2 = 6
 
      def some_inner_func():
 
          # var3 is in the nested local
 
          # namespace
 
          var3 = 7
 
  如下图所示,相同的对象名称可以出现在多个名称空间中,因为同一名称空间之间的隔离是由名称空间维护的。

  但在某些情况下PHP命名空间,您可能只对更新或处理全局变量感兴趣,如下面的示例所示,应该将其显式标记为全局变量和更新或处理。
 
  # Python program processing
 
  # global variable

  count = 5
 
  def some_method():
 
      global count
 
      count = count + 1
 
      print(count)
 
  some_method()
 
  产出:
 
  6
 
  Python中的对象范围:
 
  范围是指可从其中访问特定Python对象的编码区域。
 
  不能从代码的任何地方访问任何特定的对象,对象的范围必须允许访问。
 
  例1:
 
  # Python program showing
 
  # a scope of object

  def some_func():
 
      print("Inside some_func")
 
      def some_inner_func():
 
          var = 10
 
          print("Inside inner function, value of var:",var)
 
      some_inner_func()
 
      print("Try printing var from outer function: ",var)
 
  some_func()
 
  产出:
 
  Inside some_func
 
  Inside inner function, value of var: 10
 
  Traceback (most recent call last):
 
    File "/home/1eb47bb3eac2fa36d6bfe5d349dfcb84.py", line 8, in
 
      some_func()
 
    File "/home/1eb47bb3eac2fa36d6bfe5d349dfcb84.py", line 7, in some_func
 
      print("Try printing var from outer function: ",var)
 
  NameError: name 'var' is not defined
 
  2 作用域
 
  2.1 概念
 
  作用域是 Python 程序可以直接访问命名空间的文本区域(代码区域),名称的非限定引用会尝试在命名空间中查找名称,作用域是静态的。
 
  2.2 种类
 
  Python 有如下四种作用域:
 
  作用域的搜索顺序:从局部——嵌套——全局——内建依次进行。
 
  Python由内向外去搜索名字 ,再通过具体代码来对作用域作进一步了解,如下所示:
 
  # 全局作用域
 
  g = 1
 
  def outer():
 
      # 嵌套作用域
 
      e = 2
 
      def inner():
 
          # 局部作用域
 
          i = 3
 
  2.3 global & nonlocal
 
  我们先来看一下全局变量与局部变量。
 
  全局变量可以在整个程序范围内进行访问。
 
  而局部变量只能在函数内部访问。
 
  通过具体示例看一下:
 
  # 全局变量
 
  d = 0
 
  def sub(a, b):
 
      # d 在这为局部变量
 
      d = a - b
 
      print('函数内 : ', d)
 
  sub(9, 1)
 
  print('函数外 : ', d)
 
  执行结果:
 
  函数内 :  8
 
  函数外 :  0
 
  利用 global 和 nonlocal 关键字修改外部作用域的变量。将上面示例中 sub() 函数中的 d 变量修改为全局变量,则需使用 global 关键字,示例如下所示:
 
  # 全局变量
 
  d = 0
 
  def sub(a, b):
 
      # 使用 global 声明 d 为全局变量
 
      global d
 
      d = a - b
 
      print('函数内 : ', d)
 
  sub(9, 1)
 
  print('函数外 : ', d)
 
  执行结果:
 
  函数内 :  8
 
  函数外 :  8
 
  如果需要修改嵌套作用域中的变量,则需用到 nonlocal 关键字。
 
  2.3.1 不使用 nonlocal
 
  def outer():
 
      d = 1
 
      def inner():
 
          d = 2
 
          print('inner:', d)
 
      inner()
 
      print('outer:', d)
 
  outer()
 
  执行结果:
 
  inner: 2
 
  outer: 1
 
  2.3.2 使用 nonlocal
 
  def outer():
 
      d = 1
 
      def inner():
 
          nonlocal d
 
          d = 2
 
          print('inner:', d)
 
      inner()
 
      print('outer:', d)
 
  outer()
 
  执行结果:
 
  inner: 2
 
  outer: 2
 
 

(编辑:PHP编程网 - 钦州站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!