Orchestration of Python object-oriented elementary tutorial

Source: Internet
Author: User

Python can be used not only for structured programming, but also for objects in advanced languages. This article will introduce a related programming solution for Python object-oriented. I hope you will understand and be interested in Python object-oriented programming through the following articles.

Class definition, the class definition uses the keyword class, followed by the class name, and ":". For example, define a Point class.

Defines an empty class with no attributes or methods

 
 
  1. class Point:  
  2.     pass  
  3. '''  

Add a class attribute (some called a class member) to the class. The class attribute is consistent with the usage method of other variables. The first use is the definition of this variable, and the attribute scope is the entire class, that is, all methods of this class can access this attribute, and define the properties x and y of the Point attribute.

Defines an empty class with no attributes or methods

 
 
  1. Class Point:
  2. # Define the x and y coordinates
  3. X=10 
  4. Y=10 
  5.  
  6. '''
  7.  

A class method is a special function. Its definition method is similar to a function, but there is a default parameter self, which indicates the instance of this Class Object and defines the class method, set the setX method and the getX method for getting x.
'''
Define a complete class, including attributes and Methods

 
 
  1. Class Point:
  2. # Define the x and y coordinates
  3. X=10 
  4. Y=10 
  5. Def setX (self, x _):
  6. Self. x=X _ 
  7. Def getX (self ):
  8. Return self. x
  9. Def setY (self, y _):
  10. Self. y=Y _ 
  11. Def getY (self ):
  12. Return self. y
  13. Def setXY (self, x _, y _):
  14. Self. setX (x _)
  15. Self. setY (y _)
  16. '''

Note: This class method uses self. x instead of x to define the class attributes. Different from C ++, python class methods do not automatically use class attributes because they must be explicitly specified using self. If only x is used, a variable x is created in the setX function and its value is set to _ x. For mutual access between class methods, you must also use self to specify the called class method.

Python also supports defining methods in addition to class definition blocks. This method satisfies the requirements of Class methods. For example, you can define a function first.

Define the class and assign the class member h

 
 
  1. outX  
  2. def out_setX(self,x_):  
  3.     self.x = x_ 

Create a Class Object and access its attributes and methods.

Creates an object of a Point.

 
 
  1. pt = Point()  

Set the value of coordinate x

 
 
  1. x = 10 
  2. y = 10 
  3. setX = out_setX 
 
 
  1. pt.x = 10 

Obtains the value of coordinate x.

 
 
  1. print pt.x  

Method of accessing objects

 
 
  1. pt.setX(20)  
  2.  
  3. '''  
  4.  

The above is the first part of creating a class object for Python object-oriented objects. I hope you can obtain relevant information from it.

Edit recommendations]

  1. How to Use python programming in nginx
  2. Knowledge about python open-source technology development
  3. Use the Python standard library to modify the search engine to obtain results
  4. Python logs require constant learning.
  5. Python logs require constant learning.

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.