473,320 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Inheritance in python

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
1 1282
bvdet
2,851 Expert Mod 2GB
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

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

Similar topics

7
by: Hung Jung Lu | last post by:
Hi, I think Microsoft did look into Python when they designed C#. (E.g. they got rid of checked exceptions of Java.) However, they followed Java in avoiding multiple inheritance (MI), which is a...
4
by: Dan Bullok | last post by:
I have a couple of classes: class Base: ... class Sub(Base): ... I want to subclass Base and Sub, i.e. define classes: class MyBase(Base):
6
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
13
by: John Perks and Sarah Mount | last post by:
Trying to create the "lopsided diamond" inheritance below: >>> class B(object):pass >>> class D1(B):pass >>> class D2(D1):pass >>> class D(D1, D2):pass Traceback (most recent call last): File...
20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
0
by: Terry Hancock | last post by:
I've been discussing PyProtocols with a a friend collaborating with me on a SF game project, about the benefits and design concept of "component architecture", and I'm a little confused by what...
16
by: devicerandom | last post by:
Hi, I am currently using the Cmd module for a mixed cli+gui application. I am starting to refactor my code and it would be highly desirable if many commands could be built as simple plugins. ...
18
by: GD | last post by:
Please remove ability to multiple inheritance in Python 3000. Multiple inheritance is bad for design, rarely used and contains many problems for usual users. Every program can be designed only...
10
by: Aaron Gray | last post by:
Wikipedia says Python has Multiple Inheritance, is this true ? http://en.wikipedia.org/wiki/Multiple_inheritance Thanks, Aaron
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.