python 变量命名空间_Python命名空间– Python变量范围

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 name


def printBob():
   name = 'Bob'  # define name in function
   print('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 value
   name = 'nabisco'

   def function_inner():
       global var  # need to declare global to modify the value
       name = 'outlet'
       var = 23
       print('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 变量命名空间

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值