Stupid way to learn Python's modules, classes, objects

Source: Internet
Author: User

The module is like a dictionary

A dictionary is the only mapping relationship in Python that uses one thing to correspond to another, the so-called Key->value.

The module contains variables and functions that you can import, and you can manipulate access to variables and functions with dots (·).

Remember to ' get the concept of y from X '.

They are similar, except for the grammatical differences.

1. In other words, there is a common pattern in Python:

(1) A container with a key =value

(2) Retrieving data from the container via key

Different points:

In the dictionary, key is a string, written as [key], and in the module as. Key, the rest of the place is identical.

Classes are like modules

You can think of a module as a special dictionary of Python, and then you can access it using the dot (*) operator.

Class is to organize a series of functions and data and place them in a container, and you can also access them using the dot (*) operator.

Classes can be used to create a number of categories similar to the base class, while the module one imports the entire project with only one module

Objects are like imported

If the class is like a module, then there should also be an import-like concept. In a class, this concept is called instantiation, and when a class is instantiated, it gets the object of a class.

Create a class;

Class MyStuff (object):

def __init__ (self):

SELF.A = ' How is it? '

def apple (self):

Print ("I am classy apple")

b = MyStuff () #类的实例化

B.apple () #调用类的方法

Print b.a# properties of the printing class

Analyzing the instantiation of a class this sentence actually python has done a lot of things inside of us:

Python Lookup MyStuff () class confirmation is defined

--->python create a new object that contains all the functions defined in MyStuff ()

--->python find out if the __init__ () function is defined in the MyStuff () class, initialize the empty object if there is one

---the __init__ () method in the >mystuff () Class A self parameter, which is the empty object we created, which can be set on the variable

---> Set arguments to SELF.A (empty object) variables, initialize this object

---> Until now python can use this empty object and assign it to variable B.

Stupid way to learn Python's modules, classes, objects

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.