I have a python code -
class Name:
-
def __init__(self, name, age):
-
self.name = name
-
self.age = age
-
-
def Information(self):
-
print ' Name: ', self.name
-
print ' Age: ', self.age
-
-
-
class Identity:
-
def __init__(self, name, age, ssn):
-
self.name = name
-
self.age = age
-
self.ssn =ssn
-
-
def Information(self):
-
print ' Name: ', self.name
-
print ' Age: ', self.age
-
print ' SSN: ', self.ssn
-
-
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 -
class Identity(Name):
-
def __init__(self, name, age, ssn):
-
Name.__init__(self)
-
self.name = name
-
self.age = age
-
self.ssn =ssn
-
-
def Information(self):
-
print ' Name: ', self.name
-
print ' Age: ', self.age
-
print ' SSN: ', self.ssn
-
But my code doesn't take the advantage of inheritance
1 1282 bvdet 2,851
Expert Mod 2GB
I have a python code -
class Name:
-
def __init__(self, name, age):
-
self.name = name
-
self.age = age
-
-
def Information(self):
-
print ' Name: ', self.name
-
print ' Age: ', self.age
-
-
-
class Identity:
-
def __init__(self, name, age, ssn):
-
self.name = name
-
self.age = age
-
self.ssn =ssn
-
-
def Information(self):
-
print ' Name: ', self.name
-
print ' Age: ', self.age
-
print ' SSN: ', self.ssn
-
-
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 -
class Identity(Name):
-
def __init__(self, name, age, ssn):
-
Name.__init__(self)
-
self.name = name
-
self.age = age
-
self.ssn =ssn
-
-
def Information(self):
-
print ' Name: ', self.name
-
print ' Age: ', self.age
-
print ' SSN: ', self.ssn
-
But my code doesn't take the advantage of inheritance
It does inherit. You need to pass name and age to Name.__init__(). - class Identity(Name):
-
def __init__(self, name, age, ssn):
-
Name.__init__(self, name, age)
-
self.ssn = ssn
-
-
def Information(self):
-
print ' Name: ', self.name
-
print ' Age: ', self.age
-
print ' SSN: ', self.ssn
-
-
>>> b = Identity("John Smith", 28, "413-88-5555")
-
>>> b.Information()
-
Name: John Smith
-
Age: 28
-
SSN: 413-88-5555
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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):
|
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'
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
by: Aaron Gray |
last post by:
Wikipedia says Python has Multiple Inheritance, is this true ?
http://en.wikipedia.org/wiki/Multiple_inheritance
Thanks,
Aaron
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
| |