Python3.2 official documentation translation-class definition syntax and Class Object

Source: Internet
Author: User

6.3 first recognized class

Class introduces some new syntax: three new object types and some new semantics.

6.3.1 class definition syntax

The simplest form of class definition is as follows:

Class ClassName:




The class definition is the same as the function definition (def statement). It must be executed before it takes effect. (Of course you can place the class definition in the if statement branch or embedded in the function)

In practice, statements in the class definition are often the definition of methods, but other statements are also allowed and sometimes useful-we will discuss this later. Defining a method in a class usually has a special form of parameter list, which is used for the call convention of the method-and this will be explained in the future.

When you enter the class definition, the new namespace will be created and used with a local scope-So all parameters related to local variables will enter the new namespace. Specifically, the function definition binds the name of a new function.

When the class definition is complete (through the end), a class object will be generated. This is the basic packaging of namespace content created by the class definition. We will learn more about class objects in the following sections. The class object is bound to the class name given at the beginning of the class definition file. (That is, the ClassName In the instance)

6.3.2 Class Object

Class objects support two types of operations: Property Reference and instantiation.

In python, attribute references use standard syntax to operate on all attribute references: obj. name. valid attribute names are all names in the class namespace when class objects are created. Therefore, if the class is defined as follows:

Class MyClass:

"A simple example class """

I = 12345.

Def f (self ):

Return 'Hello world'

MyClass. I and MyClass. f are valid property references. Each of them returns an integer and function object. Class attributes can also be assigned values, so you can change the value of MyClass. I by assigning values. _ Doc _ is a Legal Attribute and returns a document string of the attribute class .; "A simple example"

Class instances use function symbols. Assume that the class object is a function without parameters, and it returns an instance of the class. Example: (assume the class above)

X = MyClass ()

Create a class instance and assign this object to the local variable x.

An empty object will be created for an instantiation operation (calling a class object. Many classes can create objects by setting specific initial states. Therefore, the class can define a special method named _ init (), as follows:

Def _ init _ (self ):

Self. data = []

When the _ init () method is defined, the _ init _ () method is automatically called for class Instantiation to create a new class instance. Therefore, in this instance, a newly initialized instance can be obtained through the following method:

X = MyClass ()

Of course, the _ init _ method has parameters for more flexible applications. In that case, the instance that gives the class instance operation will be passed to the init method. For example:

>>> Class Complex:

... Def _ init _ (self, realpart, imagpart ):

... Self. r = realpart

... Self. I = imagpart


>>> X = Complex (3.0,-4.5)

>>> X. r, x. I


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: 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.