Python Neutron class inherits the __init__ method instance of the parent class _python

Source: Internet
Author: User
Tags class definition

Objective

Students who have used python to write object-oriented code may be familiar with __init__ methods that ,__init__ run immediately when an object in a class is created. This method can be used to do some of the initialization you want for your object.

Note: This name starts and ends with a double underline.

Parent Class A

Class A (object):
 def __init__ (self, name):
  self.name=name
  print "Name:", Self.name
 def getName (self ): Return
  ' A ' + self.name

Subclass is not overridden __init__ , the parent class definition is automatically invoked when the subclass is instantiated.__init__

Class B (A):
 def getName (self): return
  ' B ' +self.name
 
if __name__== ' __main__ ':
 b=b (' Hello ')
 Print B.getname ()

Perform

$python lei2.py 
name:hello
B Hello

When overridden, __init__ instantiates a subclass and does not call the parent class that has already been defined__init__

Class A (object):
 def __init__ (self, name):
  self.name=name
  print "Name:", Self.name
 def getName (self) : Return
  ' a ' + Self.name

class B (a):
 def __init__ (self, name):
  print "HI"
  self.name = name
 def GetName (self): return
  ' B ' +self.name

if __name__== ' __main__ ':
 b=b (' hello ')
 print b.getname ()

Perform

$python lei2.py 
hi
B Hello

In order to be able to use or extend the behavior of the parent class, it is best to show methods that invoke the parent class __init__

Class A (object):
 def __init__ (self, name):
  self.name=name
  print "Name:", Self.name
 def getName ( Self): return
  ' A ' + Self.name

class B (a):
 def __init__ (self, name):
  super (B, self). __INIT__ (name)
  print "Hi"
  self.name = name
 def getName (self): return
  ' B ' +self.name

if __name__== ' __main__ ':
 b=b (' hello ')
 print b.getname ()

Perform

$python lei2.py
name:hello
hi
B Hello

Summarize

The above is about the Python subclass inherits the parent class the whole content of the __init__ method, hope this article's content to everybody's study or work can bring certain help, if has the question everybody may the message exchange.

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.