Class characteristics of Pyton

Source: Internet
Author: User

The Python class resembles a container and is a dynamic container that can add content to it at any time: its functions, its members.

For example, the default is to create an empty Python class, and if you forget to add a member function to it, you can do so.


Class Empty:

Pass


1. Add constructors

#这里必须要传入一个参数

#该参数也就是调用方的输入数据

#这里就是Empty的一个实例对象


def Empty_con (E):

The print ' empty object starts to construct '

Print (Type (e))


empty.__init__ = Empty_con


Here we test:

e1 = Empty ()

The results of the discovery are as follows:

The empty object starts to construct the

<type ' instance ' >


2. For the same reason, we can add (or replace) his destructor

def empty_des (E):

The print ' object starts to deconstruct '

Print (Type (e))


empty.__del__ = Empty_des


Here we also test the destructor:

E2 = Empty ()

del E2

The output is as follows:

' The object begins to deconstruct

<type ' instance ' >


3. We can also add a member variable to a class (type) by analogy.

When you can also add members to objects only

empty.x = 100

EMPTY.Y = 200

by calling dir (empty) you can clearly see the member structure below empty.









Class characteristics of Pyton

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.