All class members in Python variables are public, and all methods are valid. There is only one exception: If your data member name is prefixed with double underscores (_ privatevar, the name Management System of Python effectively uses it as a private variable.
In this way, there is a convention that if a variable only needs to be used in a class or object, it should be prefixed with a single underscore. Other names are public and can be used by other classes/objects. Remember that this is just a convention, and it is not required by Python to be different from the Double underline prefix). Similarly, note that the _ del _ method is similar to the destructor concept.
There are two types of domains: class variables and object variables. They are differentiated based on whether the class or object owns the variable. Class variables are shared by all object instances of a class. There is only one copy of the class Python variable, so when an object changes the class Python variable, this change will be reflected in all other instances.
The object variables are owned by each object/instance of the class. Therefore, each object has its own copy of this domain, that is, they are not shared. In different instances of the same class, although the object variables have the same name, however, they are unrelated. Using an example will make it easy to understand the variables that use classes and objects.
- #!/usr/bin/python
- # Filename: objvar.py
-
- class Person:
- '''Represents a person.'''
- population = 0
-
- def __init__(self, name):
- '''Initializes the person's data.'''
- self.name = name
- print '(Initializing %s)' % self.name
-
- # When this person is created, he/she
- # adds to the population
This is a long example, but it helps to describe the nature of the variables of classes and objects. Here, population belongs to the Person class, so it is a class variable. The name variable belongs to the object and uses self to assign values. Therefore, it is the variable of the object.
We can see that the _ init _ method uses a name to initialize the Person instance. In this method, we increase population by 1 because we have added a person. We can also find that the value of self. name is specified based on each object, which indicates the essence of self. name as the object variable.
Remember, you can only use the self variable to refer to the Python variables and methods of the same object. This is called attribute reference. In this program, we also see that docstring is equally useful for classes and methods. We can use Person. _ doc _ and Person. sayHi. _ doc _ at runtime to separate document strings of the handler class and method.
Like the _ init _ method, there is also a special method _ del __, which is called when the object disappears. The object is no longer used, and the memory occupied by the object will be returned to the system for use. In this method, we simply subtract 1 from Person. population.
When the object is no longer used, the __del _ method runs, but it is difficult to ensure when the method runs. If you want to specify how it runs, you have to use the del statement, as we used in previous examples.
- How to embed Python into C ++ applications?
- In-depth discussion of Ruby and Python syntax comparison
- Introduction to Python
- Python Learning Experience: version, IDE selection and coding Solutions
- Analysis of Python GIL and thread security