Basic introductory knowledge of Python classes _python

Source: Internet
Author: User
Tags inheritance in python
Copy Code code as follows:

Class Account (object):
"A simple Class"
Account_type= "Basic"
def __init__ (self,name,balance):
"Initialize a new account instance"
Self.name=name
Self.balance=balance
def deposit (Self,amt):
Deposit
Self.balance=self.balance+amt
def withdraw (Self,amt):
Atm
Self.balance=self.balance-amt
DEF inquiry (self):
"Return Current balance"
Return self.balance

Where the __init__ function is the constructor in Python. In addition, balance this variable is a variable of the class instance.
In addition, defining a member function in a class in Python the first argument is always self, representing an instance of itself, similar to the this pointer in C + +, but the this pointer in C + + is implicitly enclosed and globally visible, but in Python it is passed as a parameter, This is another feature of the class defined in Python.
Another feature is that in a member function of a class, a class name must be specified, as follows:
Copy Code code as follows:

Class Foo (object):
def bar (self):
Print "bar!"
def spam (self):
Bar (self) # error, throw Nameerror, can be: Self.bar
Foo.bar (self) # Legal

2. Static method of sound time in class and use static method
To use a static method in a class, precede the class member function with the @staticmethod tag to indicate that the following member function is a static function. The advantage of using static methods is that you do not need to define an instance to use this method: In addition, multiple instances share this static method, as follows:
Copy Code code as follows:

Class Simclass ():
@staticmethod
Def sharestr ():
Print "This is a static method"
Simclass.sharestr () #使用静态函数

3. Class method:
Class methods are different from ordinary member functions and static functions, as if they have not been seen in the language in which they are contacted, and see its definition:
A class method can be invoked by a class or its instance, whether you call this method with a class or a class instance, and the first argument of the method always defines the class object of the method.
Remember that the first parameter of a method is a class object, not an instance object.
By convention, the first parameter of a class method is named the CLS. It is not necessary to define class methods at any time (the functions that can be implemented by a class method can be implemented by defining a common function, as long as the function accepts a class object as a parameter).
Define class methods and use class methods:
Copy Code code as follows:

Class Abase (object):
@classmethod #类方法修饰符
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

In other words, class methods are not necessary, and the use of normal functions can also implement the functionality of class methods.
4. Inheritance of Classes
In Python, you inherit a class, like this:
Class A (object) #继承object类
#.......
Class B (A) #继承A类
#........
In addition, Python supports multiple inheritance, for multiple inheritance, to find a corresponding function, its python has a corresponding method, such as:
Copy Code code as follows:

Class D (oject): Pass #D继承自object
Class B (D): #B是D的子类
Varb = 42
def method1 (self):
Print "Class b:method1"
Class C (D): #C也是D的子类
Varc = 37
def method1 (self):
Print "Class c:method1"
def method2 (self):
Print "Class c:method2"
Class A (b,c): #A是B和C的子类
VarA = 3.3
def method3 (self):
Print "Class a:method3"

What happens if I want to call A.METHOD1 ()? The answer is CLASSB:METHOD1. The book is a kind of introduction:
When searching for a property defined in a base class, Python uses the principle of depth precedence and searches according to the order of the base class in the subclass definition. * * NOTE * * (the New-style class has changed this behavior). In the example above, if you visit A.varb, you will search in the order of a-b-d-c-d, and stop the search if you find it. If more than one base class defines the same property, only the first property value is found:
5. Data hiding
Implementing data hiding in Python is simple, and you don't need to add any keywords in front of you. The function of data hiding can be realized by adding two underscores to the class variable name or member function so that the variable name and member function are not available for instance of class, and are also hidden for the inheriting class of the class. , its inheriting class can define its identical variable name or member function name without causing a naming conflict.
Copy Code code as follows:

Class A:
def __init__ (self):
self.__x = 3 # self._a__x
Class B (A):
def __init__ (self):
A.__init__ (self)
self.__x = Notoginseng # self._b__x
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.