<python> modules, classes, objects

Source: Internet
Author: User

Python is a "object oriented Programming Language", a "OO programming language." The idea is that Python has a structure called class that allows you to construct your software in a special way. By using class, you can make your program architecture more uncluttered and cleaner to use-at least in theory. Now I'm going to teach you the basics of object-oriented programming, and I'll introduce you to object-oriented programming, classes, and objects with the knowledge you've learned. The problem is that disguised object programming (OOP) is a strange thing in itself, you only have to work hard to understand the content of this chapter, write the code well, and then go to the next section of the exercise, I can pin oop like nails into your mind. Let's get started now. modules and dictionaries are almostYou know how to create and use a dictionary this data type, which is a way of mapping one thing to another. This means that if you have a dictionary with a key called ' Apple ' in it, and you want to take a value from it, you need to do this:  mystuff = {' Apple ': ' I AM apples! '}  print mystuff[' Apple ']  remember this concept of "get X from Y", now look at the module, you've created and used some of the modules, and you've seen some of their properties: 1. A module is a Python file that contains functions and variables. 2. You can import this file. 3. You can then use the '. ' operator to access the functions and variables in the module. If I had a module named mystuff.py and put a function called apple in it, like this:  # this goes in Mystuff.py def Apple ():     Print "I AM apples!"   Then I can use import to invoke this module and access to the Apple function: Import mystuff mystuff.apple ()   I can also put a variable called Tangerine into the module inside:  def Apple ():    print "I AM apples!"  # This is just a variable tangerine = "Living reflection of a dream" I still have access to this variable:  import mystuff m Ystuff.apple ()  print Mystuff.tangerine back to the concept of the dictionary, you will find that this is a little similar to the way the dictionary is used, except that the syntax is different, we come to a ratio: mystuff[' Apple '] # Get Apple from Dict mystuff.apple ()  # get Apple from the module mystuff.tangerine # same thing, it ' s J UST a variable in other words, PythonThe side has such a generic mode: 1. Take a data container similar to Key=value style 2. Get the value in the key by its name for the dictionary, key is a string, and the syntax to get the value is [key]. For a module, key is the name of a function or variable, and the syntax is. Key. Apart from this, they are basically no different. classes and modules are almostModules can also be understood in one way: you can use them as a special dictionary through which you can store some Python code that you can access through the '. ' operator. Python has another code structure for similar purposes: class, through which you can put a set of functions and data into a container and use the '. ' operator to access them. If I want to create a class using the method of creating the MyStuff module, the method is roughly the same: Class MyStuff (object): Def __init__ (self): Self.tangerine = "and now a T Housand years between "Def" Apple (self): print "I AM classy apples!" This is a little bit more complicated than the module, indeed, there are a lot of things to do here than the module, but you should be able to figure it out almost like a mini module named MyStuff, which has a function called Apple (), which is difficult to understand __init__ () Functions, as well as the use of Self.tangerine syntax when setting tangerine variables. The reasons for using classes instead of modules are as follows: You can take the above class, repeat the creation of many, even if it is 1 million at a time, they will not interfere with each other. And for the module, when you import once, the whole program is only such a content, only to tinker very deep to get some tricks out. Before you figure this out, however, you need to understand what the object is and how to use MyStuff to achieve the same results as the import MyStuff. object equivalent to the import of the mini versionIf the class and the Mini module are similar, then for the class, there must be an import-like concept. The concept name is "instance (instance)". This is just a acting knowledgeable, it means "create". When you "instantiate" a class, you get an object. The way to instantiate is to invoke a class as if it were called a function:  thing = MyStuff ()  thing.apple ()  print thing.tangerine  the first line of code is "instantiation" Operation, which is similar to calling a function. However, when you instantiate, Python does a series of work behind it, and here's a detailed explanation for the code above: 1. Python sees MyStuff () and knows it's a class you've defined. 2. Python creates an empty object that contains all the functions you create in the class with def. 3. Then Python goes back and checks if you have created a __init__ magic function in it, and if you create it, it will call this function to initialize your empty object. 4. In the __init__ function in MyStuff, we have an extra function called self, which is the empty object that Python created for us, and I can do it like a module, a dictionary, etc., and set some variables in it. 5. Here, I set the self.tangerine to a lyrics so I initialized the object.  6. Finally, Python assigns the newly created object to a variable called thing for later use. This is the process of Python completing this "mini import" When you invoke the class like a function call. Remember that this is not a class to use directly, but a class as a "blueprint", and then use it to create a copy of the same property as this class. To remind you, my explanation and Python's actual principle is still a little bit out of the way, but here, based on your existing knowledge of the module, I can only explain this for the time being. In fact, classes and objects and modules are completely different things. If I tell you the truth, I'll probably say these things: • A class is like a blueprint, or a predefined thing, through which you can create new mini-modules. • The process of instantiation is equivalent to creating such a mini-module, and import it at the same time. • The resulting mini-module is an object that you can assign to a variable and follow. And through this series of exercisesClasses and objects and modules are already very different, so the content here is just for you to understand the concept of the class. get something from somethingNow I have three ways to get its content from something: # dict style mystuff[' apples ' # module style mystuff.apples () print Mystuff.tangerine # class Sty Le thing =mystuff () thing.apples () print Thing.tangerine

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