简体   繁体   English

处理类和对象变量时python中字符串和数字的不同行为

[英]different behavior of strings and numbers in python when dealing with class and object variables

I found one issue and not able to understand the reason of difference: 我发现了一个问题,但无法理解差异的原因:

code1: 代码1:

class Test:
    var=2
    def __init__(self):
        self.var=self.var+1

p=Test()
print "p.var:",p.var
q=Test()
print "q.var:",q.var

output 1: 输出1:

p.var:3
q.var:3

Why not the output is(According to concept used to explain code2) 为什么不是输出(根据用于解释代码2的概念)

p.var:3
q.var:4

Code2: 代码2:

class Test:
    var=[]
    def __init__(self):
        self.var.append("fool")

p=Test()
print "p.var:",p.var
q=Test()
print "q.var:",q.var

output2: 输出2:

p.var: ['fool']
q.var: ['fool', 'fool']

i read the article on code2 in Stack Exchange: python class instance variables and class variables 我在Stack Exchange中阅读了有关code2的文章: python类实例变量和类变量

but not able to link code1 with the following concept.Please help 但无法将code1与以下概念链接。请帮助

The difference here is that lists are mutable objects; 此处的区别在于列表是可变对象; integers are immutable. 整数是不可变的。 When code1 increments self.var, it must return a new object, that being 3. On the second call,we start over with 2, producing another 3 for object q . 当code1递增self.var时,它必须返回一个新对象3。在第二个调用中,我们从2重新开始,为对象q生成另一个3。

In code2, var is still a class object (only one for the class, not one per object). 在code2中, var仍然是一个类对象(该类仅一个,每个对象一个)。 When we create p , we append "fool" to the empty list. 当创建p时 ,我们将“ fool”附加到空列表中。 When we later create q we append a second "fool". 稍后创建q时,我们附加了第二个“傻瓜”。 Print them both: 同时打印它们:

p=Test2()
print "p.var1:",p.var
q=Test2()
print "q.var2:",q.var
print "p.var2:",p.var

output: 输出:

p.var1: ['fool']
q.var2: ['fool', 'fool']
p.var2: ['fool', 'fool']

Does that clarify things? 这样可以澄清事情吗?

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

相关问题 Python 将不同的变量分配给 class object - Python assign different variables to a class object Python:类和实例变量的行为 - Python : Behavior of class and instance variables 在pickle一个类时,我在cython中的python中得到了不同的行为 - When pickling a class I get different behavior in python that in cython 使用 function 或 class 在 Python 中实现装饰器时的不同行为 - different behavior when implementing a decorator in Python with a function or a class 从不同的文件导入类时,Python 3是否存在意外行为? - Python 3 isinstance unexpected behavior when importing class from different file? Python-使用字符串列表作为类数据成员。 当字符串包含变量时,变量不会更新 - Python - Using list of strings as class data member. When strings contain variables, variables don't update 在将Python 2代码移植到Python 3时处理ctypes和ASCII字符串 - Dealing with ctypes and ASCII strings when porting Python 2 code to Python 3 在Python中处理大量实例属性时的代码设计 - Code Design when Dealing with Large Numbers of Instance Attributes in Python 在处理复数和numpy时如何在python中正确指定dtype? - How to specify dtype correctly in python when dealing with complex numbers and numpy? 使用递归函数时处理 python 全局变量 - dealing with python global variables when using recursive functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM