Basic operation of attributes in Python object-oriented programming

Source: Internet
Author: User

In object-oriented programming, properties and methods are used very frequently. The following is a simple summary of the basic operation of the attribute.

In Python oop, attributes are typically manipulated in the following ways:

1, add attributes

2, modify the value of the property

3, get the value of the property

4, delete properties

Use a simple demonstration code to illustrate:

Class DemoClass:

Pass

obj = DemoClass ()

# Add new Attribute

Obj.value = 123

# get the value of attribute

Print (Obj.value)

# Modify the value of

Obj.value = 456

Print (Obj.value)

# Delete Attribute

Del Obj.value

Try

Print (Obj.value)

Except

Print ("error")

In the above code, each annotation explains what the corresponding operation means. In the above code, a simple demonstration of the properties of the Add, modify, get the value and delete several operations. The deleted property is definitely inaccessible, so a try statement avoids the error that might be thrown.

The results of the procedure are as follows:

Grey@desktop-3t80npq:/mnt/e/01_workspace/02_programme_language/03_python/03_oop/2017/08$python attr_demo.py

123

55W

Error

From the above results, roughly do the following summary:

1, directly to the object plus a property assignment operation, if the property does not exist will create a new property;

2, for the object's property assignment operation, if the property already exists, will modify the operation;

3, the property of the numerical acquisition, similar to the C language data structure;

    4, the corresponding information cannot continue to be referenced after the property has been deleted.

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.