Example Analysis of Class usage in Python and Analysis of python instances

Source: Internet
Author: User

Example Analysis of Class usage in Python and Analysis of python instances

This example describes the usage of the Class in Python. We will share this with you for your reference. The details are as follows:

Although Python has advantages that other languages cannot match in Function Programming, we should not forget that Python is also an OO language. Therefore, while focusing on the advantages of Python in FP, we also need to understand Python's OO features.

To discuss the OO features of Python, it is naturally the first thing to understand the Class in Python. Defining classes and creating object instances in Python is simple. The specific code is as follows:

Class GrandPa: def _ init _ (self): print ('I \'m grandpa') class Father (GrandPa): def _ init _ (self ): print ('I \' m Father! ') Class Son (Father): "A simple example class" I = 12345 def _ init _ (self): print (' This is the constructor, son ') def sayHello (self): return 'Hello world' if _ name _ =' _ main _ ': son = Son () # print type help information ('Type help information: ', Son. _ doc _) # print ('Type name: ', Son. _ name _) # print ('base class inherited by the type', Son. _ bases _) # print ('Type dictionary: ', Son. _ dict _) # print ('module of Type: ', Son. _ module _) # print instance type ('instance type: ', Son (). _ class __)

The running effect is as follows:

Python 3.3.2 (v3.3.2: d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32Type "copyright", "credits" or "license () "for more information. >>> ================================== RESTART ======== ==========================================>>> this is the constructor, son type help information: A simple example class type name: Son type inherited base class: (<class '_ main __. father '>,) type dictionary: {' _ module _ ':' _ main _ ', 'sayhello': <function Son. sayHello at 0x010194F8>, '_ doc _': 'A simple example class', '_ init _': <function Son. _ init _ at 0x010194B0>, where the 'I': 12345} type is located: _ main _ this is the constructor, son instance type: <class '_ main __. son'>

Python supports multiple inheritance

First, you will find a bracket in the definition of the Class, which is the embodiment of 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;

_ Init _ is the constructor in the Class.

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;

Python member variables

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 the Data Memebers in the 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?

Python method Ambiguity

Fourth, because Python supports multiple inheritance, the problem of method ambiguity may occur [1]. 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.

Let's start with this. The OO features in Python will be further described in future Post.

Method ambiguity: because a class inherits two or more parent classes at the same time, there are methods with identical signature in these parent classes, then the compiler will not be able to determine which parent class the subclass will inherit, resulting in method ambiguity.

I hope this article will help you with Python programming.

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.