Python Basic Learning-object-oriented use of classes

Source: Internet
Author: User

Oop Object-oriented

1. Class
-Variable
-Class variables
Class variables are common throughout the instantiated object. Class variables are defined in the class and outside the body of the function. Class variables are not typically used as instance variables.
A class variable is also called a static variable, which can be called directly using the class name without instantiating the class, or it can be called by an object
-member variables
Can be called by the object of the class, and the member variable must be given as self, because the meaning of itself is represented by the instantiated object
-Instance variables
A variable defined in a method that acts only within the current method
-
-Method
-Construction method
class defines the __init__ () method, the instantiation operation of the class automatically calls the __init__ () method
-Class method
Inside the class, use the DEF keyword to define a method that, unlike a generic function definition, must contain the parameter self, which is the first argument, and self represents an instance of the class.
-Static method
Methods created with @classmethod above the method name are static methods, static methods cannot use the self parameter, class methods and static methods can access static variables (class variables) of the class, but cannot access instance variables
-Private method
__private_method: Two underscores, declares that the method is private, can only be called inside the class, and cannot be called outside the class.

To force access to private variables and private methods:
Class Test (object):
def __siyou (self):
Print ("Private Method")
A = Test ()
A._test__siyou () #强制访问test类的私有方法__siyou ()

The proprietary methods of the class:

__init__: constructor, called when generating an object
__DEL__: Destructors, using when releasing objects
__REPR__: printing, converting
__SETITEM__: Assigning values by index
__getitem__: Getting values by index
__LEN__: Get length
__CMP__: comparison operation
__CALL__: Function call
__ADD__: Add operation
__sub__: minus operation
__MUL__: Multiply operation
__DIV__: Except operations
__mod__: Finding the remainder operation
__POW__: exponentiation

Python Basic Learning-object-oriented use of classes

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.