Python Learning note 016--Object-oriented

Source: Internet
Author: User
Tags instance method

Object-oriented refers to the use of classes to describe an object (instance), the use of classes to establish the relationship between instances and instances

Objects: Instance of object: instance

1 class 1.1 What is a class

Classes are tools used to describe objects, and classes can be used to create one or more homogeneous objects. Classes: Class

1.2 Role of the class

Used to describe an object

Used to create homogeneous objects with the same properties

1.3 Creation of classes
class class Name (inherited list): "" "        Class document string    " ""    instance method (function methed within Class) defines a    class variable definition    class method (@classmethod) defines a    static method (@ Staticmethod) Definition

Attention:

1. The class name must be an identifier

2, the class name is essentially a variable, it binds a class

3, the definition of the class to add two blank lines to tell the interpretation of the executor, the definition of the class has started and ended

4, in a Python program, each instance can have its own variables, each instance variable has its own independent scope

Example

# define a class class Car:     Pass # Create a car instance c1 == car ()

In fact, we often use the list function is also a class, in the definition of the list (A = list ()), is actually the process of instantiation.

1.4 Syntax invocation of an instance variable
# Create or modify an instance variable v = instance. Variable name  #  Gets the instance variable bound object

Attention:

The first assignment of an instance variable is created and is assigned again to change the binding relationship of the variable

2 Example methods
class class name (Inherit list):     def instance method name (self, parameter 1, parameter 2, ...):         " the document string for the instance method "         statement block

Attention:

1. The essence of the instance method is the function, which is exactly the function defined within the class.

2. The instance method belongs to the property of the class.

3. The first parameter of the instance method is to invoke the instance of the method, which is generally named self

4. If there is no return statement in the instance method, return None

2.1 Invocation of an instance method
Instance. Instance method name (invocation parameter) class name. Instance method name (instance, call parameter)

Both of the two methods are available.

3 Construction methods

The constructor method is also called the initialization method.

Syntax format

class class name (Inherit list):     def __init__ (self [, parameter list]):        statement block

Attention:

1. The constructor method name must be init.

2, each class can only be used by a constructor init function.

3. The construction method is called automatically when the instance is generated and the instance itself (as an argument) to the self parameter in init.

4. If there is no return statement in the constructor method, return None

4 destructor method

Refactoring method syntax

syntax format:   class class name (Inherit list):       def __del__ (self):          statement block

Attention:

1. The destructor method is called automatically when the object is destroyed.

2, in the Python language, it is recommended not to do anything when the object is destroyed, because the time of destruction is difficult to determine

Python Learning note 016--Object-oriented

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