How to reload and call Python basic functions

Source: Internet
Author: User
Tags inheritance in python

I have not been familiar with the Python language for a long time. I am not very familiar with many features of this language, and I still do not know many usage methods. Today, I want to write a function call in the inheritance of Python object-oriented programming. Share and make progress together.

Because I have been familiar with Java and C ++ before, I have been familiar with all object-oriented ideas. Here, we will not repeat what Object-oriented means. Let's go directly to the code! Let's see how to call the inheritance and base-class functions in Python ~

First, it is the base class file base. py

The code is as follows: Copy code
'''
Created on Dec 18,201 4

@ Author: raul
'''

Class animal (object ):
'''
Classdocs
'''


Def _ init _ (self ):
'''
Constructor
'''
Print 'animal init'
       
Def say (self ):
Print 'animal say'


Then, it is the child. py subclass file.

The code is as follows: Copy code
'''
Created on Dec 18,201 4

@ Author: raul
'''
From inheritance. base import animal

Class cat (animal ):
'''
Classdocs
'''


Def _ init _ (self ):
'''
Constructor
'''
# Animal. _ init __()
Animal. _ init _ (self)
Print 'cat init'
       
Def say (self ):
Animal. say (self)
Print 'cat say'


If _ name _ = '_ main __':
C = cat ()
C. say ()


After running, you can see the output as follows:

Animal init
Cat init
Animal say
Cat say


This indicates that our inheritance and function call are OK.

This example is relatively simple, but I have basically understood the reload and call of Python-based functions. I hope you can give a similar picture.

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.