python类中的静态变量 [英] Static variables in python classes

查看:235
本文介绍了python类中的静态变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解Python类中的静态变量的概念时遇到问题。根据 Python中的静态类变量,只要我们在方法外部和内部定义变量, python类,此变量是静态的。这意味着可以访问该变量,而无需实例化类中的对象,并且可以直接通过类名进行访问。
例如:

I have a problem in understanding of the notion of static variables in Python classes. According to Static class variables in Python, whenever we define a variable outside a method and inside a python class, this variable is static. This means this variable can be accessed without any need to instantiate an object from a class and can be accessed by the class name directly. For example:

class my_class:

     i=12

     def __init__(self,j):
        self.j=j


instance=my_class(10)

my_class.i:
>12
instance.i:
>12
instance.i=13
instance.i:
>13
my_class.i:
>12 

您可以看到我们可以访问静态变量 i 通过实例对象和类名。但是,当我们更改实例对象的 i 的值时,它不会影响该类的值( my_class.i 仍为12)。
另一方面,如果我们使用数组静态变量,情况将完全改变。
考虑类似的示例:

You can see that we can access the static variable i through both instance object and the class name. However, when we change the value of i for instance object it does not affect the value of the class(my_class.i is still 12). On the other hand, things totally change if we are working with array static variables. Considering the similar example:

class my_class:

     i=[]

     def __init__(self,j):
        self.j=j


instance=my_class(10)

my_class.i:
>[]
instance.i:
>[]
instance.i.append(13)
instance.i:
>[13]
my_class.i:
>[13]

您可以看到,当我更改实例对象数组的变量时,它也会影响类的值。这里发生了什么?如果有人可以帮助我更好地理解这个问题,我将不胜感激,因为这对我而言并不那么明显。顺便说一下,我有Java背景。

You can see that when I change the variable for the array of instance object it also affects the class value. What is going on here? I would appreciate if someone could help me better understand this issue as it is not that much obvious to me. By the way, I have a Java background.

推荐答案

分配给实例属性通常设置实例属性。一旦有了实例属性,它就会屏蔽类属性。

Assignment to an instance attribute normally sets an instance attribute. Once there is an instance attribute, it masks the class attribute.

因此,在执行 instance.i = 13 之前,实例上没有 i 属性,仅在类上。然后,将实例上的名称 i 绑定到 13 ,并在下次查找 instance.i 该属性被找到,并且不再尝试查找 my_class.i

So before you executed instance.i=13, there was no i attribute on the instance, only on the class. You then bound the name i on the instance to 13, and the next time you look up instance.i that attribute is found, and no attempt is made to find my_class.i anymore.

但是操作可变对象与分配给类或实例属性不同。您没有分配给 instance.i ,而是更改了 my_class.i 引用的列表对象,并可以通过<$看到c $ c> instance.i 。 instance.i 仍然会找到 my_class.i 属性,您从未使用过 = 创建 instance.i 属性。

But manipulating a mutable object is not the same thing as assigning to a class or instance attribute. You did not assign to instance.i, you altered the list object referenced by my_class.i and visible through instance.i. instance.i still will find the my_class.i attribute, you never used = to create the instance.i attribute.

这是因为您只有读过 instance.i 对列表的引用,以便打印出来并找到 list.append()方法。您绝对不会为<$​​ c $ c> instance.i 属性引用设置新值。

That's because you only ever read the instance.i reference to the list, in order to print it, and to find the list.append() method. At no point do you set a new value for the instance.i attribute reference.

列表对象是自己的对象,可以通过添加或删除或替换列表的 indices 引用的值来更改该对象。不论哪个名称或属性引用该列表,您都可以在同一列表中使用任意数量的此类引用,并且更改列表不会更改这些引用。

The list object is it's own object, one that you can alter by adding or removing or replacing the values referenced by the indices of the list. It doesn't matter what names or attributes reference that list, you can have any number of such references to the same list, and altering the list won't change those references.

尝试创建对该列表的更多引用,然后看看会发生什么:

Try creating more references to the list, and see what happens:

>>> list_named_i = instance.i
>>> list_named_i
[13]
>>> my_class.i.append(42)
>>> list_named_i
[13, 42]
>>> list_named_i[0] = 81
>>> instance.i
[81, 42]

instance.i my_class.i list_named_i 都是对同一列表对象的不同引用,

instance.i, my_class.i and list_named_i are all just different references to the same list object, and you can modify (mutate) that list object via any of those references.

我建议您通读Python名称,属性和列表以及此类的工作方式;请参阅Ned Batchelder的 关于Python名称和值的事实和神话

I recommend you read up on how Python names and attributes and lists and such work; see Facts and myths about Python names and values by Ned Batchelder.

这篇关于python类中的静态变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆