By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

Inheritance in python

P: 90
I have a python code
Expand|Select|Wrap|Line Numbers
  1. class Name:
  2.       def __init__(self, name, age):
  3.             self.name = name
  4.             self.age = age
  5.  
  6.       def Information(self):
  7.             print ' Name: ', self.name
  8.             print ' Age: ', self.age
  9.  
  10.  
  11. class Identity:
  12.       def __init__(self, name, age, ssn):
  13.             self.name = name
  14.             self.age = age
  15.             self.ssn =ssn
  16.  
  17.       def Information(self):
  18.             print ' Name: ', self.name
  19.             print ' Age: ', self.age
  20.             print ' SSN: ', self.ssn
  21.  
  22.  
I want to use inheritance structure, Name as parent class, Identity as child class. Without chaning the parent class code, how can I rewrite the child class code (Identity class) so I can reuse all functions from parent class?

I can try
Expand|Select|Wrap|Line Numbers
  1. class Identity(Name):
  2.       def __init__(self, name, age, ssn):
  3.             Name.__init__(self)   
  4.             self.name = name
  5.             self.age = age
  6.             self.ssn =ssn
  7.  
  8.       def Information(self):
  9.             print ' Name: ', self.name
  10.             print ' Age: ', self.age
  11.             print ' SSN: ', self.ssn
  12.  
But my code doesn't take the advantage of inheritance
Nov 26 '07 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
I have a python code
Expand|Select|Wrap|Line Numbers
  1. class Name:
  2.       def __init__(self, name, age):
  3.             self.name = name
  4.             self.age = age
  5.  
  6.       def Information(self):
  7.             print ' Name: ', self.name
  8.             print ' Age: ', self.age
  9.  
  10.  
  11. class Identity:
  12.       def __init__(self, name, age, ssn):
  13.             self.name = name
  14.             self.age = age
  15.             self.ssn =ssn
  16.  
  17.       def Information(self):
  18.             print ' Name: ', self.name
  19.             print ' Age: ', self.age
  20.             print ' SSN: ', self.ssn
  21.  
  22.  
I want to use inheritance structure, Name as parent class, Identity as child class. Without chaning the parent class code, how can I rewrite the child class code (Identity class) so I can reuse all functions from parent class?

I can try
Expand|Select|Wrap|Line Numbers
  1. class Identity(Name):
  2.       def __init__(self, name, age, ssn):
  3.             Name.__init__(self)   
  4.             self.name = name
  5.             self.age = age
  6.             self.ssn =ssn
  7.  
  8.       def Information(self):
  9.             print ' Name: ', self.name
  10.             print ' Age: ', self.age
  11.             print ' SSN: ', self.ssn
  12.  
But my code doesn't take the advantage of inheritance
It does inherit. You need to pass name and age to Name.__init__().
Expand|Select|Wrap|Line Numbers
  1. class Identity(Name):
  2.       def __init__(self, name, age, ssn):
  3.             Name.__init__(self, name, age)
  4.             self.ssn = ssn
  5.  
  6.       def Information(self):
  7.             print ' Name: ', self.name
  8.             print ' Age: ', self.age
  9.             print ' SSN: ', self.ssn
  10.  
  11. >>> b = Identity("John Smith", 28, "413-88-5555")
  12. >>> b.Information()
  13.  Name:  John Smith
  14.  Age:  28
  15.  SSN:  413-88-5555
  16. >>> 
Nov 27 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.