Object-oriented Python (i)

Source: Internet
Author: User

I.. dir built-in function
    1. Enter A. After the identifier/data, and then press the TAB key, Ipython prompts the list of methods that the object can invoke
    2. After using the built-in function dir to pass in identifiers/data, you can view all the properties and methods within the object
    3. # View the Comments demo. __doc_
Ii. Class 1, definition

The name of the big hump of class names

2. Create
Object variable = class name ()
3. Self parameter (which object invokes method, self is the reference to which object is similar to this)

(1), add attribute

# directly through the external code of the class. Set a property to " Tom "

(2), call properties

    • Self. Accessing the properties of an object
    • Self. Calling other object methods

(PS This method in the external assignment of properties is not recommended to use OH)

(3) Initialization method

The initialization method is called automatically when an object is created using the class name __init () __

Example:

(4) Built-in method

    • __del__ method
      • Del object (can delete an object) if not active del then the object will be executed after executing all code del
    • __str__ method
      • What to output when the object is output (must be a string)
      • PS (within a pair of parentheses in Python can automatically connect when the line is changed)

4. Identity operator

Compares the memory addresses of two objects---whether a reference to the same object

When compared to none in Python, it is recommended to use is to determine

    • is similar ID (x) = = ID (y)
    • is not similar to ID (x) = = ID (y)
Third, private properties and methods

1. Definition method

when defining properties and methods, add two underscores to the property name and method name , which is defined as a private property or method

2. Pseudo-private properties and private methods

Python does not have a real sense of private

In fact, just give the name a few special treatment, so that the outside world can not access to

How to handle: precede the name with the _ class name _ Class name __ Name

Object-oriented Python (i)

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.