Python 3.x Learning note 10 (destructor and inheritance)

Source: Internet
Author: User

1. Use of class variables:
Common properties for everyone, save overhead (memory)

2. Destructors
Performed when an instance is released and destroyed, usually for some finishing touches, such as closing some database links and opening temporary files

3. Private methods
The two underscore begins with the declaration that the method is a private method and cannot be called outside of a class.

4. Private properties
The two underscore begins with the declaration that the property is private and cannot be used outside of the class or accessed directly.


When inheriting, rewrite the constructor to write all the parameters of the parent class once plus the child class variable, then call the parent class, and then add the instantiation variable of the subclass.

The python2.x Classic class is inherited by depth, and the new class is inherited by the breadth of the first.

python3.x Classic classes and modern classes are all inherited by breadth first.


1 #Parent Class 12 classPerson (object):#New Style3 4     def __init__(self,name,age):5         #constructor = Name#instantiate a variable (static property), scoped to instantiate itself7Self.age = Age8Self.friends = []9 Ten     defEat (self):#class method functionality (Dynamic properties) One         Print('%s'll eat something!' A  -     defRun (self): -         Print('%s'll runing!' the  -     defSleep (self): -         Print('%s'll sleep!' -  + #Parent Class 2 - classRelation (object): +     defmake_friends (self,obj): A         Print('%s make friend with%s'%(, atSelf.friends.append (obj)#the argument here is obj, in this example, obj is both a W1 -  -  - #sub-class - classMan (person,relation):#Multiple Inheritance -     def __init__(Self,name,age,money):#overriding Constructors in         #person.__init__ (Self,name,age,money) #经典写法 -Super (Man,self).__init__(Name,age)#new style of writing, it is suggested to use this = Money +  -     defPiao (self): the         Print('%s is piaoing ....' *  $ Panax Notoginseng #sub-class - classWoman (person,relation):#Multiple Inheritance the     defPiao (self): +         Print('%s is piaoing ....' A  theM1 = Man ('Zhang San', 20,10) +  -W1 = Woman ('Lili'21st) $  $ m1.make_friends (W1) - Print(M1.friends[0].name) - Print(m1.friends[0].age) the  - #M1.piao ()Wuyi () the () - #M1.sleep ()

Python 3.x Learning note 10 (destructor and inheritance)

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: 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.