Python class inheritance and initialization usage analysis of subclass instances _python

Source: Internet
Author: User

The examples in this article describe the use of Python class inheritance and subclass instance initialization. Share to everyone for your reference. The specific analysis is as follows:

[The original reference books (Chinese and English)]
__init__ Method Introduction:
If a base class has an __init__ () method The derived class "s __init__ () method must explicitly call it to ensure prope R initialization of the base class part of the instance; For example: "Baseclass.__init__ (self, [args ...])"
As a special contraint on constructors, no value may be returned; Doing so'll cause a typeerror to is raised at runtime.

If its base class also has __init__ (), it must be explicitly invoked at __init__ () to ensure that its base class part is properly initialized; for example: "Baseclass.__init__ (self, [args ...])" As a special case of a constructor, it has no value returned, and if a value is returned, an exception TypeError is thrown at run time.

1. Subclasses define the __init__ method, if the call to the base class __init__ method is not displayed, Python will not help you invoke, explain run all OK

Class A ():
  def __init__ (self):
    print ' A '
class B (a):
  def __init__ (self):
    print ' B '
if __name_ _== ' __main__ ':
  b=b ()
>>> 
b

2. When subclasses do not define the __init__ method, Python automatically invokes the first base class __init__ method. That is, when a subclass inherits from multiple base classes, only the __init__ method of the first base class is invoked to:

Class A:
  def __init__ (self):
    print ' A '
class B:
  def __init__ (self):
    print ' B '
class C (b):
  def __init__ (self):
    print ' C '
  pass
class D1 (a,b,c):
  Pass
class D2 (b,a,c):
  Pass
class D3 (c,b,a):
  pass
if (__name__== ' __main__ '):
  print ' d1-------: '
  d1=d1 ()
  print ' d2-------: '
  d2=d2 ()
  print ' d3-------: '
  d3=d3 ()
>>> 
d1------- :
a
d2-------:
B
D3-------:
C

3 when the __init__ method is not defined by the base class, if the subclass shows the invocation of the base class __init__ method, Python goes up to the __init__ method of the base class of the basis classes and invokes it in real terms with 2

Class A:
  def __init__ (self):
    print ' A '
class B:
  def __init__ (self):
    print ' B '
class C1 (b,a):
  Pass
class C2 (a,b):
  Pass
class D1 (C1):
  def __init__ (self):
    c1.__init__ (self)
Class D2 (C2):
  def __init__ (self):
    c2.__init__ (self)
if (__name__== ' __main__ '):
  print ' D1-------: '
  d1=d1 ()
  print ' D2-------: '
  d2=d2 ()
>>> 
d1-------:
b
D2-------:
A

I hope this article will help you with your Python programming.

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.