Home  >  Article  >  Backend Development  >  Are there member variables in python?

Are there member variables in python?

(*-*)浩
(*-*)浩Original
2019-06-20 10:18:212533browse

There are two main types of variables used in python classes: class variables and member variables. Class variables are common to all instantiated objects of the class, while member variables are unique to each instantiated object.

Are there member variables in python?

#The following is explained through two small programs. (Recommended learning: Python video tutorial)

class A(object):
    def __init__(self):
        #aa为成员变量
        self.aa = 10

    @staticmethod
    def test(self):
        self.aa += -1
if __name__ == '__main__':
    x = A()
    y = A()
    #调用x
    x.test(x)
    print x.aa #输出9
    y.test(y)
    print x.aa #输出9
    print y.aa #输出9

We can obtain it in the destructor as self.aa, but obviously, aa at this time is in the form of a member variable appears, the modifications made to it at this time can only be directed to its object itself and will not affect other class objects. I think this design should be more consistent with the definition of a destructor, otherwise when an object exits the scope, it will be a particularly dangerous thing for other objects.

For more Python related technical articles, please visit the Python Tutorial column to learn!

The above is the detailed content of Are there member variables in python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Is python a crawler?Next article:Is python a crawler?