[英]Python: What is the difference between a global variable, vs. a variable with the prefix “self.”, vs. a local variable?
I've been exposed to C/C++/Java like syntax over the years, and the way that Python variables are defined just sort of confuses me. 多年来,我已经接触过类似C / C ++ / Java的语法,而Python变量的定义方式让我感到困惑。 Can anyone describe what the differences are among the three mentioned in the q?
任何人都可以描述q中提到的三个差异吗?
A global variable is just that -- a variable that is accessible globally. 全局变量就是 - 全局可访问的变量。 A local variable is one that is only accessible to the current scope, such as temporary variables used in a single function definition.
局部变量是仅可由当前范围访问的变量,例如单个函数定义中使用的临时变量。 An instance variable (eg: when using the
self.
prefix) is data that is associated with a specific instance of an object. 实例变量(例如:当使用
self.
前缀时)是与对象的特定实例相关联的数据。 Of course, you can also reference instance objects outside of the object by using object.x
where object
is a reference to that object. 当然,您也可以使用
object.x
引用对象外部的实例对象,其中object
是对该对象的引用。
If a variable is prefixed with self
, it is neither local nor global. 如果变量以
self
为前缀,则既不是本地变量也不是全局变量。 It is part of the makeup of a specific instance of an object. 它是对象的特定实例的构成的一部分。 Roughly speaking, an instance variable represents a property of a specific object.
粗略地说,实例变量表示特定对象的属性。
In the following example, lx
is a local variable, local to the method greet
. 在以下示例中,
lx
是一个局部变量,是方法greet
的本地变量。 gx
is a global variable accessible anywhere in the module, ix
is an instance variable that could have a unique value for each instance of the object. gx
是可在模块中的任何位置访问的全局变量, ix
是一个实例变量,可以为对象的每个实例提供唯一值。 When referenced inside of the object definition you would refer to ix
with the prefix self
, and when outside the object with a prefix of the object reference. 在对象定义内部引用时,您将引用带有前缀
self
ix
,以及带有对象引用前缀的对象外部。
gx = "hello"
class Foo:
def __init__(self, who):
self.ix = who
def greet(self):
lx = "%s, %s" % (gx, self.ix)
return lx
foo = Foo("world")
print foo.greet()
print foo.ix
self
works just like the this
from C++ and Java. self
工作就像this
从C ++和Java。 You get a reference to an object that you can then access with the .
您将获得对可以随后访问的对象的引用
.
operator ( ->
in C++). 运算符(
->
在C ++中)。
In Python the only way of accessing instance variables is explicitely through self
. 在Python中,访问实例变量的唯一方法是通过
self
明确表达。 Instance variables are not placed in the same "normal" scope as local or global variables are. 实例变量不会放在与本地或全局变量相同的“正常”范围内。
Sometimes it is really useful to return to first principles: 有时返回第一原则非常有用:
When we talk about a variable being local or global we are referring to its 'scope' (or context). 当我们谈论变量是本地变量或全局变量时,我们指的是它的“范围”(或上下文)。 Variables inside a function are considered local to the function.
函数内部的变量被认为是函数的本地变量。 Here are illustrations from Swaroop's excellent guide, ' A Byte of Python ':
以下是Swaroop优秀指南“ A Byte of Python ”的插图:
x = 50
def func(x):
print('x is', x)
x = 2
print('Changed local x to', x)
func(x)
print('x is still', x)
output: 输出:
x is 50 x是50
Changed local x to 2 将本地x更改为2
x is still 50 x仍然是50
Next is an illustration of how a global variable traverses context between the inside and outside of a function: 接下来说明全局变量如何遍历函数内部和外部之间的上下文:
x = 50
def func():
global x
print('x is', x)
x = 2
print('Changed global x to', x)
func()
print('Value of x is', x)
output: 输出:
x is 50 x是50
Changed global x to 2 将全局x更改为2
Value of x is 2 x的值是2
That is all there is to it. 这就是它的全部。
When we utilize the Object Oriented programming aspects of Python, then the notion of ' self ' becomes relevant. 当我们利用Python的面向对象编程方面时,“ 自我 ”的概念变得相关。 Class instances and instances are considered to be 'objects' and the ' self .'
类实例和实例被认为是“对象”和“ 自我” 。 prefix allows our code to differentiate between whether we are referring to a local/global variable or to attributes of an object.
前缀允许我们的代码区分我们是引用本地/全局变量还是引用对象的属性。
Once again, the concept is explained with zen-like simplicity by Swaroop . 再一次, Swaroop用禅宗般的简洁解释了这个概念。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.