Summary of inheritance and method rewriting for Python classes

Source: Internet
Author: User
Tags stdin

Pythonclass inheritance and method rewrite summary

We all know that classes can inherit, and inheritance allows code to be reused to make the code look more concise

Like what:

Class B (A):P

defines a name for the Class B, which inherits from a, we call B the subclassof A, a superclass called b (parent class).

Method overrides

When a subclass defines a method with the same name as a superclass, the subclass's method overrides the same method as the superclass (or rewrite)

Let's borrow two examples:

>>> class Bird: ... def __init__ (self): ... self.hungry = True ... def eat (self): ... if self . Hungry: ... print ' aaaah ... self.hungry = False ... else: .... print ' No,than Ks! ' ...>>> B = Bird () >>> dir (b) >>> b.eat () aaaah...>>> b.eat () no,thanks!>>>

This class defines the basic functions of birds: eating

define a class again, SongBird is Bird 's subclass,SongBird can sing .

>>> class SongBird (Bird): ... def __init__ (self): ... self.sound = ' squawk! ' ... def sing (self): ... print self.sound ...
>>> sb = SongBird () >>> sb.sing () squawk!>>> sb.eat () Traceback (most recent call last): File "&L T;stdin> ", line 1, <module> File" <stdin> ", line 5, in Eatattributeerror:songbird instance have no attr Ibute ' Hungry ' >>>

SongBird is a subclass of Bird, he inherited the eat method of the superclass, but the call when the error, the hint does not have the hungry attribute, why this?

reason is the construction method of SongBird __init__ () overrides that there is no code for the Hungry property in the new constructor . To achieve the desired effect,the constructor method of the SongBird must call the parent class's constructor to ensure basic initialization. There are two ways to do this: call the unbound version of the superclass's construction method, or use the Super function.

The unbound version of the constructor method that called the superclass

>>> class SongBird (Bird): ... def __init__ (self): ... Bird.__init__ (self) ... self.sound = ' squawk! ' ... def sing (self): ... print self.sound ...

SongBird class adds only one line of code bird.__init__ (self)

Take a look at the results.

>>> sb = SongBird () >>> sb.eat () aaaah...>>>

when a method of an instance is called, the self parameter of the method is automatically bound to the instance (this is called the binding method), but if the method of the class is called directly (such as bird.__init__), no instance is bound. Such a method is called an unbound method.

by supplying the current instance as the self parameter to an unbound method,the SongBird class can use all implementations of its superclass construction method.

Super function

the Super function can only be used in modern classes. The current class and object can be used as arguments to the Super function, and any method that invokes the object returned by the function is called a method of the superclass, not the method of the current class.

then you don't have to the construction method of SongBird uses Bird, and the direct use of super (songbird,self) Note is a comma, not .

In addition, the__init__ method can be called in a normal (binding) way

>>> __metaclass__ = type>>> class Bird: ... def __init__ (self): ... self.hungry = True ... Def eat (self): ... if self.hungry: .... print ' Aaaah ... self.hungry = False ... el SE: ... print ' no,thanks! ' ...

>>> class SongBird (Bird): ... def __init__ (self): ... super (songbird,self). __init__ () ... Self.s Ound = ' squawk! ' ... def sing (self): ... print self.sound...>>> sb = SongBird () >>> sb.eat () aaaah...>>> ; Sb.eat () no,thanks!


Summary of inheritance and method rewriting for Python classes

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.