Python object-oriented (1), python object-oriented

Source: Internet
Author: User

Python object-oriented (1), python object-oriented
I. dir built-in functions II. Class 1. Definition

Big camper naming method

2. Create
Object variable = Class Name ()
3. self parameter (which object calls method, and reference of which object is self is similar to this)

(1) Add attributes

# In the external code of the class, you can directly set an attribute through. To do this, tom. name = "Tom"

(2) Call attributes

  • Self. Access Object Attributes
  • Self. Call other object Methods

 

(PS is not recommended for external attribute assignment)

(3) initialization method

When an object is created using the class name, the initialization method _ init () _ is automatically called ()__

Example:

 

(4) built-in Methods

  • _ Del _ Method
    • Del object (an object can be deleted) If del is not active, the object performs del after all code is executed.
  • _ Str _ Method
    • Output content when the object is output (must be a string)
    • PS (in Python, a pair of brackets can automatically help with the connection when the line is changed)

4. Identity Operators

Compare whether the memory addresses of two objects are consistent-whether to reference the same object

When comparing None in Python, we recommend that you use is to judge

  • Is similar to id (x) = id (y)
  • Is not similar to id (x) = id (y)
3. Private attributes and Methods

1. Definition Method

When defining attributes and methods, add two underscores before the attribute name and method name to define private attributes or methods.

2. Pseudo-Private attributes and private methods

Python does not really mean private

In fact, it only makes some special processing for the name, making it inaccessible to the outside world.

Processing Method: Add _ Class Name (I .e. _ class name_name) before the name

 

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.