Notes for using class variables and member variables in python: python Variables

Source: Internet
Author: User

Notes for using class variables and member variables in python: python Variables

Preface

Recently I was writing a project in python and found a disgusting bug, that is, the data between two instances generated by the same class could affect each other, which made me very puzzled. Later I thought that java classes have class variables and instance variables, so I read the relevant information and found that python also has similar class variables and instance variables. Let's take a look at the details.

See the following sample code:

class A: x = 0 def __init__(self): self.y = 0

X is a class variable, and y is an instance variable.

In principle, there is no error, but some disgusting problems are found in actual use (that is, I found a three-day bug )... For example, the following code:

class A: x = [] y = 0 def __init__(self): pass def add(self): self.x.append('1') self.y+=1a=A() print a.x,a.yprint A.x,A.ya.add()print a.x,a.yprint A.x,A.yb=A() print b.x,b.yprint A.x,A.y

It is obvious that both x and y are class variables, and add is used to modify x and y respectively. Create an instance a, modify the value of instance a, and construct instance B.

I thought this result was obvious, but his output was:

[] 0[] 0['1'] 1['1'] 0['1'] 0['1'] 0

Where is the problem? Obviously, both x and y are class variables. In the second group of print, why is a. x and B. x the same, but a. y and B. y are different?

After thinking for a long time, I realized a truth... For python, class variables are indeed common to all classes. But that is when we use the same reference. For example, the append method of the [] object shares a class variable. But for the value assignment statement, if a value assignment statement is used for class variables in the class, python will generate a copy of the object. Subsequent operations will be based on this copy without affecting the original Class Object. This explains the above phenomenon.

In order to prevent the variables from being shared when you forget the differences between the class variables and the instance variables, the best way is to reinitialize the variables when they are used in each class, this will not cause an accident.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message, thank you for your support.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.