Python3.2 official documentation translation-class remarks and exceptions are also class

Source: Internet
Author: User

6.7 remarks

Sometimes there is a data type like "record" in Pasca and "data body" in C that is very useful. Set some data items. An empty class definition can be clearly displayed:

Class Employee:

Pass

John = Employee () # Create an empty employee record

# Fill the fields of the record

John. name = 'John Doe'

John. dept = 'computer lab'

John. salary = 1000

It is expected that a python code block of the special abstract round type can be passed to the class. This class can mimic that data type method. For example, if you have a function for formatting data from a file object, you can define a method that includes the read and readline methods to obtain data and pass it as a parameter.

The instance method object also has many attributes: m. _ self _ is a sorting object containing the method m (), and m. _ func _ Is the function object corresponding to the method.

6.8 exception is also a class

User-defined exceptions can also be defined by classes. This mechanism can be used to implement scaling exceptions.

Here there are two valid (syntax) forms to throw the statement:

Raise Class

Raise Instance

In the first form, the Class must be an instance of type or the Class inherits it. The first form is the following convenient method:

Raise Class ()

In an exception, the class and the exception are compatible. The exception here refers to the same class or a base class (but cannot be reversed-the exception statement in the traversal derived class cannot be compatible with the base class) for example, BCD will be printed in sequence.

Note that if the exception statement is flipped (before the occurrence of T), it will print out BBB, and the first exception statement that meets the requirements will be triggered.

When an error message for an unhandled exception is printed, the exception class name is printed. Next there are semicolons and spaces, and finally the built-in method is used to convert the instance into a string


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.