In-depth analysis of Python object-oriented language

Source: Internet
Author: User

In our programs, we all design programs based on functions or statement blocks that operate on data. This is called Python object-oriented programming, and we also combine data and functions, it is called the function of object convergence. This method is called the Python object-oriented programming concept.

You can use procedural programming most of the time, but sometimes when you want to write large programs or find a more appropriate solution, you have to use object-oriented programming technology. Class and object are two main aspects of Python object-oriented programming.

Class to create a new type, and the instance of the Class Object. This is similar to a variable of the int type, which stores the integer variable as an instance object of the int class ). Note to C/C ++/Java/C # programmers that even integers belong to the int class ). This is different from that of C ++ and Java.

Use help (int) to learn more about this class. C # and Java 1.5 programmers will be familiar with this concept because it is similar to the concept of encapsulation and solution encapsulation. Objects can use common variables that belong to objects to store data. A variable of an object or class is called a field. Objects can also use functions of classes.

  • Instructions on executing the Python Interpreter
  • How to Use the Python modifier to decorate Functions
  • Introduction to the Python Editor interface
  • Exploring unusual Python programs
  • How to create multiple Python inheritance

Such a function is called a class method. These terms help us distinguish them from isolated functions and variables. Fields and methods can be collectively called class attributes. There are two types of domain: the object that belongs to each instance/class or the class itself. They are called instance variables and class variables respectively. Class is created using the class keyword. Class fields and methods are listed in an indention block.

Class methods have only one special difference from common functions-they must have an additional first parameter name, but you do not assign a value to this parameter when calling this method, python provides this value. This special variable refers to the object itself, which is named self by convention.

Although you can give this parameter any name, it is strongly recommended that you use the self name-other names are not in favor of you. Using a standard name has many advantages-your program reader can quickly identify it if you use self.

Some integrated IDE development environments can also help you. Comments to C ++/Java/C # programmers. self in Python is equivalent to self pointer in C ++ and this reference in Java and C. You must be wondering how Python assigns values to self and why you don't need to assign values to it.

This can be clarified by an example. Assume that you have a class named MyClass and an instance named MyObject of this class. When you call this object method MyObject. method (arg1, arg2), this will be automatically converted from Python to MyClass. method (MyObject, arg1, arg2) -- this is the principle of self.

This also means that if you have a method without parameters, you still have to define a self parameter for Python object-oriented. The following example shows a simple class:

 
 
  1. #!/usr/bin/python  
  2. # Filename: simplestclass.py  
  3.  
  4. class Person:  
  5.     pass # An empty block  
  6.  
  7. p = Person()  
  8. print p  

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.