Basic knowledge of [Python] classes

Source: Internet
Author: User
Document directory
  • 1. Definition of Classes
  • 3. Class methods:
  • 5. Data Hiding
1. Class account (object ):
"A simple class"
Account_type = "Basic"
Def _ init _ (self, name, balance ):
"Initializing a new account instance"
Self. Name = Name
Self. Balance = balance
Def deposit (self, AMT ):
"Deposit"
Self. Balance = self. Balance + AMT
Def withdraw (self, AMT ):
"Withdrawal"
Self. Balance = self. Balance-AMT
Def inquiry (Self ):
"Return Current Balance"
Return self. Balance

The __init _ function is the constructor in Python. In addition, the balance variable is a variable of the class instance.

In addition, in Python, the first parameter for defining a member function is always self, indicating its own instance, which is similar to the this pointer in C ++, however, the this pointer in C ++ is invisible and globally visible, but must be passed as a parameter in Python. This is another feature of defining classes in Python.

Another feature is that when using another member function in the class, you must specify the class name as follows:

Class Foo (object ):
Def bar (Self ):
Print "bar! "
Def spam (Self ):
Bar (Self) # The nameerror is thrown, which can be self. Bar.
Foo. Bar (Self) # valid

2. Static Method in class sound and use static method

To use static methods in a class, add the @ staticmethod mark before the class member function to indicate that the following member functions are static functions. The advantage of using the static method is that you can use this method without defining an instance: In addition, multiple instances share this static method as follows:

Class simclass ():
@ Staticmethod
Def sharestr ():
Print "this is a static method"


Simclass. sharestr () # use static functions 3. Class methods:

The class method is different from the common member functions and static functions. In the language of contact, it seems that you have never seen such semantics. Let's look at its definition:

A class method can be called through a class or its instance. Whether you use a class to call this method or a class instance to call this method, the first parameter of the method always defines the Class Object of the method.
Remember: The first parameter of a method is a class object rather than an instance object.
By convention, the first parameter of the class method is named Cls. defining class methods is not necessary at any time (all functions that can be implemented by class methods can be implemented by defining a common function, as long as this function accepts a class object as a parameter ).
Define the class method and use the class method:

Class abase (object ):
@ Classmethod # class method Modifier
Def aclassmet (CLS): Print 'a class method for ', CLS. _ name __
Class aderiv (abase): Pass
Binstance = abase ()
Dinstance = aderiv ()
Abase. aclassmet () # prints: A class method for abase
Binstance. aclassmet () # prints: A class method for abase
Aderiv. aclassmet () # prints: A class method for aderiv
Dinstance. aclassmet () # prints: A class method for aderiv

That is to say, class methods are not necessary. Using common functions can also implement the functions of class methods.

4. class inheritance

In python, inherit a class like this:
Class A (object) # inherit the object class
#.......
Class B (a) # inherit Class
#........

In addition, Python supports multi-inheritance. For multi-inheritance, find a corresponding function. Python has corresponding methods, such:

Class D (oject): pass # D inherits from object
Class B (d): # B is a subclass of D.
Varb = 42
Def Method1 (Self ):
Print "Class B: Method1"
Class C (d): # C is also a subclass of D.
Varc = 37
Def Method1 (Self ):
Print "Class C: Method1"
Def method2 (Self ):
Print "Class C: method2"
Class A (B, c): # A is a subclass of B and C.
Vara = 3.3
Def method3 (Self ):
Print "Class A: method3"

What will happen if I want to call a. Method1? The answer is classb: method1. the book only introduces the following:
When you search
When defining an attribute in a class, Python adopts the depth-first principle and searches by the base class sequence in the subclass definition. ** Note ** (the new-style class has changed this behavior ).
In the preceding example. varb, the search will be in the order of A-B-D-C-D, as long as the search is found to stop. if multiple base classes define the same attribute
A property value found:

5. Data Hiding

In python, Data Hiding is simple. You do not need to add any keywords in front of the class variable name or member function. You only need to add two underscores before the class variable name or member function to hide the data.
For instances, its variable names and member functions cannot be used, and the inherited classes of its classes are also hidden, its inherited class can define its identical variable name or member function name without causing
Name Conflict.

Class:
Def _ init _ (Self ):
Self. _ x = 3 # self. _ a1_x

Class B ():
Def _ init _ (Self ):
A. _ init _ (Self)
Self. _ x = 37 # self. _ b1_x

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.