A deep dive into python -- class

Source: Internet
Author: User

Python class is divided into three parts: class and object (class and object), inheritance (inheritance), overload (overload) and override (overwrite ).
Class and object
Class Definition, instantiation, and member access. By the way, classes in python all inherit from a class called object.

class Song(object):#definition    def __init__(self, lyrics):        self.lyrics = lyrics#add attribution    def sing_me_a_song(self):#methods        for line in self.lyrics:            print linehappy_bday = Song(["Happy birthday to you",                   "I don't want to get sued",                   "So I'll stop right there"])#object1bulls_on_parade = Song(["They rally around the family",                        "With pockets full of shells"])#object2happy_bday.sing_me_a_song()#call functionbulls_on_parade.sing_me_a_song()

Inheritance (inheritance)
Python supports inheritance and multi-inheritance, but it is generally not recommended to use multi-inheritance because it is not safe!
class Parent(object):    def implicit(self):        print "PARENT implicit()"class Child(Parent):    passdad = Parent()son = Child()dad.implicit()son.implicit()

Overload and override)
Overload and override exist in both types of static language, such as C ++, Java, and C.
Although python is a dynamic language, it also supports heavy load and overwrite.
However, unlike C ++, python uses Parameters Default ValueTo implement function overloading. Next we will introduce an example of heavy load in C ++, and then provide the corresponding python implementation.
Example of C ++ function overloading:
Void f (string str) // output string str 1 time {cout <
 
  
Python implementation:
Overload by default parameter values
def f(str,times=1):       print str*timesf('sssss')f('sssss',10)


Overwrite
class Parent(object):    def override(self):        print "PARENT override()"class Child(Parent):    def override(self):        print "CHILD override()"dad = Parent()son = Child()dad.override()son.override()

Super () function
After a function is overwritten, how do I call a function of the parent class?
class Parent(object):    def altered(self):        print "PARENT altered()"class Child(Parent):    def altered(self):        print "CHILD, BEFORE PARENT altered()"        super(Child, self).altered()        print "CHILD, AFTER PARENT altered()"dad = Parent()son = Child()dad.altered()son.altered()

In python, does the subclass automatically call the parent class _ init _ () function?
The answer is no. The subclass must use the super () function to call the _ init _ () function of the parent class.
class Child(Parent):    def __init__(self, stuff):        self.stuff = stuff        super(Child, self).__init__()




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.