Python Class correct application code example analysis

Source: Internet
Author: User

As we all know, although the Python programming language is simple and easy to use, its functions are quite powerful. Especially in the object-oriented aspect, the advantage is more obvious. We will introduce the Python Class application method in detail today to deepen our understanding of this aspect.

To discuss the OO features of Python, understanding Python Class is naturally at the forefront. Defining classes and creating object instances in Python is simple. The specific code is as follows:

 
 
  1. class MyClass(BaseClassA, BaseClassB):  
  2. def __init__(self):  
  3. self.x = 0 
  4. self.y = 0 
  5. def __init__(self, x, y):  
  6. self.x = x  
  7. self.y = y 

"The preceding constructor is incorrectly used. For the correct usage, see FeedBack from Qiangning Hong"

 
 
  1. myClass = MyClass()  
  2. print myClass.x, myClass.y  
  3. myClass.z = 10 
  4. print myClass.z  
  5. del myClass.x 

After reading the above Code, do you have many problems? Okay, let me answer it one by one.

  • Reading XML documents from Python
  • Analysis of Python multi-thread programming
  • Python thread synchronization functions in practical applications
  • Analysis of the correct application method of Python synchronization queue
  • Brief introduction to the basic application method of calling Python scripts

First, you will find a parentheses in the definition of Python Class, which reflects the inheritance. Java uses extends, C #, C ++ uses colons (:), and Python uses parentheses. There are two values in the brackets. You can find that Python supports multiple inheritance;

The second, __init _, is the constructor in the Class. The two types of constructor reflect Python's support for function overloading. In constructor, a special parameter self has the same meaning as this, which is common in Java and C. Here, we need to emphasize that the method defined in the Class is actually a function, but it must include the self parameter in the method definition and put the self parameter first;

Third, in Python, you do not need to explicitly declare the Data Members of the Class. Instead, when assigning values, the assigned variables become the Data Memebers of the Class, as x and y in the code. Not only do you not need to explicitly declare Data Members, but you can even delete Data Memebers in Python Class using the del method. When I first saw this feature, I was taken aback. After all, the first option of OO is encapsulation. But does this destroy the encapsulation feature?

Fourth, because Python supports multi-inheritance, method ambiguity may occur. However, because Python follows the deep-first search rule, the problem of method ambiguity is well avoided. For example, in the above Code, MyClass inherits from BaseClassA and BaseClassB at the same time. Assume that MyClass calls a method called derivedMethod. The derivedMethod is defined in BaseClassA and BaseClassB at the same time, and the Signature is identical, the method in BaseClassA will be called. If the BaseClassA does not define the derivedMethod, but the BaseClassA parent class defines this method, the derivedMethod in the parent class of BaseClassA will be called.

In short, the path for the Inheritance Method Search is from left to right. After a BaseClass is selected, the BaseClass will be searched along the inheritance structure until the top, then go to another BaseClass.

Related Article

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.