Python review-Review 6 Lessons (December 3)

Source: Internet
Author: User
Tags python script

Review 6 sessions (December 3)
2.5/2.6 module Use
2.7 Object-oriented introduction
Properties of Class 2.8
Method of Class 2.9
Built-in Methods for class 3.0
Inheritance of the 3.1/3.2 class

Notes:


Module:

Modules are the basic way to organize your code in Python.
A python script can run on its own, or it can be imported into another script, which we call a module when the script is imported to run.
All. py files can be imported as a module
The module name is the same as the file name of the script
For example, if we write a script named hello.py, you can import it in another script with an import Hello statement.


Package:
Python modules can be organized into packages by directory
Steps to create a package:
-Create a directory with the name of the package name
-Create a __init__.py file in this directory
-Store script files or compiled extensions and sub-packages in this directory, as needed
-Import pack.m1, pack.m2, pack.m3

Python Path
Sys.path

Export PYTHONPATH

Summarize:
The module is a Python script file that can be imported;
Package is a number of directory-organized modules and sub-packages, the directory has a __init__.py file, this file can hold the information of the package;
Syntax for importing modules and packages:
-Import, import as
-From ... import ...


Object-Oriented Introduction:

Object-oriented programming--object oriented programming, short for OOP, is a programming idea. OOP takes objects as the basic unit of a program, and an object contains functions for data and manipulating data.

Process-oriented programming treats a computer program as a set of commands, which is the sequential execution of a set of functions. In order to simplify the program design, the process will continue to cut the function into sub-function, that is, the large function by cutting into small block function to reduce the complexity of the system.

Object-oriented programming treats a computer program as a set of objects, and each object can receive messages from other objects and process them, and the execution of a computer program is a series of messages passing between objects.

In Python, all data types can be treated as objects and, of course, objects can be customized. The custom object data type is the concept of class in object-oriented.

Properties of the class:

The properties of a class are divided into public and private properties by scope of use, and the class's property scope depends on the name of the property.
-Public Property: A property that can be called in a class and outside the class.
-Private property: cannot be called outside the class and by a function other than the class.
Definition: A member variable that starts with a double underscore is a private property
can be accessed via Instance._classname__attribute mode.
-Built-in properties: The system is added by default when defining the class, consisting of a double underscore, __dict__, __module__.


Methods of the class:

The definition of a method is the same as a function, but it requires self as the first argument.
The class method is:
-Public methods
-Private method
-Class method
-Static method


Public method: A method that can be called in a class and outside of a class.
Private method: cannot be called by the outside of the class, adding "__" in front of the method double underline is the private method.
Self parameter
The method used to differentiate between functions and classes (must have a self), which represents the execution of the object itself.


Class method: A function that is processed by the Classmethod () function, can be called by a class, and can be called by an object (an inherited relationship)
Static methods: Equivalent to "global functions", can be called directly by the class, can be shared by all instantiated objects, defined by Staticmethod (), static methods do not have "self" parameter.
Adorner:
-@classmethod
-@staticmethod



The built-in methods of the class:

Method 1: Call the inner class directly using an external class
object_name = Outclass_name.inclass_name ()
Method 2: Instantiate the external class first, and then instantiate the inner classes
Out_name = Outclass_name ()
In_name = Out_name.inclass_name ()
In_name.method ()

Inheritance of the class:
That is, a derived class (derived class) inherits the fields and methods of the base class. Inheritance also allows the object of a derived class to be treated as a base class object. For example, there is a design where an object of type dog is derived from the animal class, which is the analog "is a (is-a)" Relationship (example, dog is a animal).

Python review-Review 6 Lessons (December 3)

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.