1. Let's take a good look at the "class" 1. Object
All the friends who have learned java know that everything in Java is an object-oriented language. What about python? Will that happen? First, let's assume it is, and then prove it. If everything is an Object in python, there must be a primitive base class, just like an object in Java. It is easy to guess whether it is also an Object? We can use interactive commands to check whether:
Indeed, we found these things through the help Command. First, this is a builtin built-in module, and second, this is a base class at the top of most base, that is to say, all classes inherit from objects. Finally, we find that the object class has many built-in attributes, so we can override these attributes in our class for our purpose.
In the above example, we call the dir and str methods of the base class. Here is a problem. we print the object directly and we call the str results of the base class. In fact, when we print an object, it automatically calls the _ str _ method. Don't believe it? Let's take another example. In this example, we overwrite the _ str _ method to see what results will be generated after the object is printed. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD48cD48aW1nIHNyYz0 = "http://www.2cto.com/uploadfile/Collfiles/20140314/20140314092600390.jpg" alt = "\"/>
In fact, we can overwrite other built-in objects, depending on the function of the class you want to write.
2. Class Method 1. Static Method
Static methods can be called directly by class or class instances. It does not have special behaviors such as the regular method (binding, unbinding, default first parameter rules, and so on ). Static methods can be viewed as a common function called using attribute reference. Defining static methods at any time is not required. Let's take a look at the definition of static methods of classes:
Static methods do not have to be instantiated (languages are common), which is basically the same as java.
In the static method of the class, we should pay attention to a method __new __. when we instantiate a class, in fact, the class calls the _ new _ method to return a Class Object. Let's try:
The isinstance method is used to determine whether Obj3 is a test object. However, we cannot directly obtain the object through _ new _, because in this case, Obj3 is only an object, which can be simply understood as follows, that is, we usually define an instance of the class. In fact, the class first calls _ new _ to generate an object, and then calls _ init _ to initialize the class. Let's verify:
2. Class methods (non-static)
The method of the class is very simple. Basically, it is similar to the function, but there is a need to note that self must be clearly pointed out (in fact, it is not related to the name, but only to the position, the General Convention is self). Let's take a look at an example:
3. Properties)
We all know that attributes in a class are equivalent to member variables. How does a class work when we reference attributes of a class externally? This will start with the _ getattr _ method, __getattr _ is used to intercept vertex number operations. To be precise, it should be, when the vertex operation is performed on undefined attribute names and instances, this method is called using the attribute name as a string. If this attribute cannot be found up through the python inheritance tree, the method will not be called. Let's look at an example:
At this time, we will definitely think about one thing, that is, can we change the value printing result? I think we can try:
But the result seems to be unavailable. What should I do? Let's try another method _ setattr __, which intercepts all attribute assignment statements. If this method is defined, self. attr = value is changed to self. _ setattr _ ('attr ', value) read a piece of code:
However, as long as you have a little bit of detail, you will find that if you assign values to self. value in _ setattr _, it will fall into an endless loop. In the end, Stack Overflow exception occurs. How can this problem be solved? Generally, we assign values to any instance attribute by performing index operations on the attribute dictionary, that is, use self. _ dict _ ['attrname'] = x, as shown below:
4. How to express the "private" attribute of class members?
In C ++, we have public protect private. How can we implement attributes in python, or even the privatization of functions? Here we will use _ X _. Let's look at a piece of code:
The same is true for attributes and methods. It's already half past four AM. It's so strange. Good night, everybody!