简体   繁体   English

类变量Vs。 python中的实例变量的int值

[英]Class variable Vs. Instance variable in python for int value

I am new to python and i am not sure how this is working. 我是python的新手,我不确定这是如何工作的。 Code is as below: 代码如下:

class test():
    d=0
    def __init__(self):
       self.d=self.d+1;

D=test()
print D.d
D1=test()
print D1.d
D2=test()
print D2.d

Output is 输出是

1,1,1  # This should not be

Now using this : 现在使用这个:

class test():
    d=[]
    def __init__(self):
       self.d.apend("1");

 D=test()
 print D.d
 D1=test()
 print D1.d
 D2=test()
 print D2.d

Result is (This should be) 结果是(应该是)

['1']
['1', '1']
['1', '1', '1']

So i am not sure why integer value is not being treated as class variable while list is being treated. 因此,我不确定在处理列表时为什么不将整数值不视为类变量。

In the first example, 在第一个示例中

self.d = self.d + 1

rebinds self.d , making it independent of test.d . 重新绑定 self.d ,使其成为一个独立的test.d

In the second example, 在第二个示例中

   self.d.append("1")

modifies test.d . 修改 test.d

To see that for yourself, print id(self.d) at the end of both constructors. 若要亲自查看,请在两个构造函数的末尾输出id(self.d)

If you modified the second example to match the first: 如果您修改了第二个示例以匹配第一个示例:

   self.d = self.d + ["1"]

you'd see that the behaviour would also change to match. 您会发现行为也会发生变化以匹配。

If you want to modify a class variable, do: 如果要修改类变量,请执行以下操作:

class test(object):
    d=0
    def __init__(self):
       type(self).d=self.d+1;

D=test()
print D.d
D1=test()
print D1.d
D2=test()
print D2.d

You don't need the type on the right hand side of the assignment, because this way you never create an instance variable d . 您不需要赋值右侧的type ,因为这样您就永远不会创建实例变量d Note that new-style classes are necessary to this. 请注意,为此需要新样式的类。

type is a function (actually a callable - it is also a class; but don't worry about that for now) which returns the class of its argument. type是一个函数(实际上是一个可调用的-它也是一个类;但是暂时不用担心),它返回其参数的类。 So, type(self) returns the class of self . 因此, type(self)返回类的self Classes are first class objects in Python. 类是Python中的一流对象。

Demo here: http://ideone.com/JdNpiV 此处演示: http : //ideone.com/JdNpiV

Update: An alternative would be to use a classmethod . 更新:一种替代方法是使用classmethod

To address a class variable use class_name.variable_name, giving : 要解决类变量,请使用class_name.variable_name,给出:

class test(object):
    d=0
    def __init__(self):
       test.d = test.d + 1;

NPE's answer tells you what is going wrong with your code. NPE的答案告诉您代码出了什么问题。 However, I'm not sure that it really tells you how to solve the issue properly. 但是,我不确定它是否真的告诉您如何正确解决问题。

Here's what I think you want, if each test instance should have a different d value in an instance variable: 如果每个test实例在实例变量中应具有不同的d值,这就是我想要的:

class test(object): # new style class, since we inherit from "object"
    _d = 0 # this is a class variable, which I've named _d to avoid confusion

    def __init__(self):
        self.d = test._d # assign current value of class variable to an instance variable
        test._d += 1     # increment the class variable

Now, you can create multiple instances and each one will get a unique value for d : 现在,您可以创建多个实例,每个实例将获得d的唯一值:

>>> D0 = test()
>>> D1 = test()
>>> D2 = test()
>>> print D0.d
0
>>> print D1.d
1
>>> print D2.d
2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM