python 变量命名空间

In this tutorial, we are going to learn about Python Namespace and variable scope. In our previous tutorial we learned about Python Custom Exception.

在本教程中,我们将学习Python命名空间和变量作用域。 在上一教程中,我们了解了Python自定义异常 。

Python命名空间 (Python Namespace)

Namespace is the naming system to avoid ambiguity and to make name uniques. If you’re familiar with other programming language (i.e C/C++/Java), you may know the namespace already.

命名空间是一种避免歧义并使名称唯一的命名系统。 如果您熟悉其他编程语言(即C / C ++ / Java),则可能已经知道名称空间。

However, Python’s namespace is implemented using Python Dictionary. That means, namespace is basically a key-value pair. For a given key, there will be a value. In the following sections, we will be discussing about names and their space.

但是,Python的名称空间是使用Python Dictionary实现的。 这意味着名称空间基本上是一个键值对。 对于给定的键,将有一个值。 在以下各节中,我们将讨论名称及其空间。

名称及其空间 (Names and Their Space)

If you dissect the word namespace, you will get two things. One is name and another is space. Basically, name is refer to the object name (also knows as identifier). That means, the object you declare is enlarge the namespace. And we have told earlier that the namespace in python is implemented using dictionary. So consider, there are two namespace, nameA and nameB. You can imagine them as

如果剖析名称空间一词,将会得到两件事。 一个是名字,另一个是空间。 名称基本上是指对象名称(也称为标识符)。 这意味着,您声明的对象是扩大名称空间。 前面我们已经说过,python中的名称空间是使用字典实现的。 因此考虑一下,有两个名称空间,即nameA和nameB。 你可以想象他们是

nameA={ ‘var_name1’=object1, ‘var_name2’=object2, …}
nameB={ ‘var_name1’=object3, ‘var_name2’=object5, ‘var_name3’=object6, …}

Here, you can see that, the names can be identical in both namespace but they are different as object. So, namespace allows us to use objects of same name but in different namespace. The following code will provide you the basic idea of namespace.

在这里,您可以看到,两个名称空间中的名称可以相同,但作为对象它们却不同。 因此,名称空间允许我们在不同名称空间中使用相同名称的对象。 以下代码将为您提供名称空间的基本概念。

name = 'Andy'  # define namedef printBob():name = 'Bob'  # define name in functionprint('printing from the def: ', name)  # print from function# the main function
print('printing from the main: ', name)  # print from the main
printBob()  # call the function to print

The produced output will be

产生的输出将是

printing from the main:  Andy
printing from the def:  Bob

So, you can see that two different object has same name but they are in different scope. Hence, their output differs.

因此,您可以看到两个不同的对象具有相同的名称,但是它们在不同的范围内。 因此,它们的输出不同。

Python变量范围 (Python Variable Scope)

Actually, scope refers the coding region from where the objects of that scope can be accessed. That means, you cannot access the objects of a particular function from anywhere of your code. Like, the following code, you will see that you cannot access an object from outside of its scope.

实际上,范围是指可以从中访问该范围的对象的编码区域。 这意味着,您无法从代码的任何位置访问特定功能的对象。 像下面的代码一样,您将看到无法从其范围之外访问对象。

def printBob():var = 'print it'print('printing from the function: ', var)  # this will print# call the function to  print the variable value
printBob()
# try to access the object from outside of its scope
print('printing from the main: ', var)  # this will produce error

So, you will see the output like below.

因此,您将看到如下输出。

That means, you can call the function from the main scope but you can’t access the object of that function from the main scope. So, here comes the concept of scope. Basically, you can consider one indented block as its scope. This means, in the following code;

这意味着,您可以从主作用域调用该函数,但不能从主作用域访问该函数的对象。 因此,这里涉及范围的概念。 基本上,您可以将一个缩进块视为其范围。 这意味着,在下面的代码中;

var = 0
name = 'absicsa'def function_outer():# global var    ; no need to declare to call the valuename = 'nabisco'def function_inner():global var  # need to declare global to modify the valuename = 'outlet'var = 23print('name :',name, ', var :', var)print('name :', name, ', var :', var)function_inner()print('name :', name, ', var :', var)
function_outer()

The output of the code will be;

代码的输出将是;

Here, you can see that, the same indented blocks consist of the same namespace. So function_outer() function can be called from the main function’s scope while the function_inner() function can be called from the function_outer() function’s scope. You can also see that global variable can be accessed from the inner namespaces.

在这里,您可以看到,相同的缩进块由相同的名称空间组成。 所以function_outer()函数可以从主功能的范围被称作而function_inner()函数可以从function_outer()函数的范围被调用。 您还可以看到可以从内部名称空间访问全局变量。

命名空间的生命周期 (Lifetime of the Namespace)

The lifetime of the namespace differs because they are created at different time. When the scope ends, the objects that are created in that scope usually deleted. That’s why, you cannot access objects offer inner namespace from the outer namespace because either they are not create yet or the namespace have already been deleted. But in the previous example, you see that you can access the objects from the outer namespace from the inner namespace.

命名空间的生存期有所不同,因为它们是在不同的时间创建的。 当作用域结束时,通常会删除在该作用域中创建的对象。 这就是为什么您不能从外部名称空间访问提供内部名称空间的对象的原因,因为它们尚未创建或名称空间已被删除。 但是在前面的示例中,您看到可以从内部名称空间访问外部名称空间的对象。

So, that’s all about the Python Namespace. Hope, you understand well. If you have any query, please use the comment box.

所以,这一切都与Python命名空间有关。 希望你很好理解。 如有任何疑问,请使用评论框。

References: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/14463/python-namespace-variable-scope

python 变量命名空间

python 变量命名空间_Python命名空间– Python变量范围相关推荐

  1. 关于python变量使用_Python 基础知识关于变量的定义使用

    转自:https://www.cnblogs.com/manongajie/p/12736991.html 1 python 变量概述 变量,英文叫做 variable. 从形式上看,每个变量都拥有独 ...

  2. python的命名空间_python命名空间(namespace)

    #命名空间(namespace)#命名空间指的是变量存储的位置,每一个变量都需要存储到指定的命名空间当中#每一个作用域都会有一个它对应的命名空间#全局命名空间,用来保存全局变量.函数命名空间用来保存函 ...

  3. python变量运算符_Python(三) 变量与运算符

    一.什么是变量 变量 = [1,2] 二.变量的命名规则 字母,数字,下划线,首字母不能是数字 系统关键字 不能用在变量名中 保留关键字 区别大小写 a=1,   a='1',   a=(1,2),  ...

  4. python变量定义问题_python 定义n个变量方法 (变量声明自动化)

    python 定义n个变量方法 (变量声明自动化) code: for i in range(100): cmd = "t%s = 1" % i exec cmd eval(&qu ...

  5. python变量分类_Python 入门系列 —— 5. 三大变量类型介绍

    多值赋给多变量 Python 允许在一行中将多个值赋给多个变量.x, y, z = "Orange", "Banana", "Cherry" ...

  6. python变量回收_Python变量的引用、拷贝和回收机制

    1.Python中变量的引用 Python中的变量都是指针,都是某个内存对象的引用. python不允许程序员选择采用传值还是传引用.Python参数传递采用的肯定是"传对象引用" ...

  7. #中regex的命名空间_Python命名空间实例解析

    Python的命名空间是Python程序员必须了解的内容,对Python命名空间的学习,将使我们在本质上掌握一些Python中的琐碎的规则. 接下来我将分四部分揭示Python命名空间的本质:一.命名 ...

  8. python冒泡排序函数_python冒泡排序-Python,冒泡排序

    arr = [ 7 , 4 , 3 , 67 , 34 , 1 , 8 ] . def bubble_sort : 最近在学习Python,下面是我的一些笔记 冒泡排序 实现思路: 使用双重for循环 ...

  9. python自动化入门_python自动化-python入门

    1.安装python,配置环境变量.windows下是安装路径直接配到环境变量里面就可以 mac添加环境变量 在~/.bash_profile这个文件中,添加一行 alias python=" ...

最新文章

  1. Java面向对象三大特征 之 多态性
  2. 2020中国一流大学名单(27所)和中国大学综合实力300强出炉!
  3. php5ts.dll 注册码,修复php5ts.dll
  4. MSMQ 安装问题的解决过程
  5. php-fpm linux 权限,nginx/php-fpm及网站目录的权限设置
  6. android 中radiogroup滑动切换,巧妙实现缺角radiogroup控制多个fragment切换和滑动
  7. python3操作excel(xls与xlsx版本的爱恨情仇)
  8. 计算机二级基础知识微盘,计算机二级C++基础知识(整理版).pdf
  9. mysql提取前两个数据_各种数据库提取表的前几条记录的方法
  10. 20140711 set
  11. ajax send()的作用_AJAX(Asynchronous JavaScript And XML)
  12. hz和分贝怎么转换_分贝换算(db换算公式)
  13. 一元线性模型的中位数回归
  14. Web前端人员如何提升能力 提高效率有哪些方法
  15. windows安装Nessus
  16. android分享微信朋友圈带编辑功能吗,终于被我等到了,微信新版本能编辑别人的朋友圈啦!...
  17. ISO 14229、ISO 15765、ISO 11898的区别
  18. js 验证码错误,输入框获得焦点并清除内容
  19. 新闻字幕条制作,一款不错的ae字幕模板
  20. office 论文 页码_WORD目录,页眉,页脚,页码设置技巧 为你的毕业论文收藏吧-以Word 2013演示...

热门文章

  1. .NET 环境中使用RabbitMQ(转)
  2. 《程序设计实践》读书笔记第五至六章
  3. luogu 4884 多少个1 (BSGS)
  4. jdbcdbcpc3p0
  5. BZOJ 1673 [Usaco2005 Dec]Scales 天平:dfs 启发式搜索 A*搜索
  6. MacOs终端忽略大小写
  7. 【DS】About Stack
  8. Generalised Dice Overlap as a Deep Learning Loss Function for Highly Unbalanced Segmentations
  9. 卡尔曼滤波器的一种形象表达
  10. 输入课程信息的C语言代码,[源码和文档分享]基于C语言的课程信息管理系统