?
Solved

Python overloading constructor question

Posted on 2009-04-22
6
Medium Priority
?
492 Views
Last Modified: 2012-05-06
I have to extend via inheritance a python class, which has a default constructor with some arguments. The sub class implements as well your own constructor with some other arguments, and somehow should get initialize the super class via argument from within its own constructor.
So the super class is first instantiated and then it will be extended via another class with some other methods.

How to solve this situation?


# super class
class c1:
   def __init__(self,arg1,arg2, arg3):
         self.prm1 = arg1
         self.prm2 = arg2
         self.prm3 = arg3
 
# extender
class c2(c1):
   def __init__(self, arg4,prm5):
         self.prm4 = arg4
         self.prm5 = arg5
 
 
a = c1(1,2,3)
b = c2(5,6)
# here instance 'b' should somehow extends the instance 'a' 
 
print b.prm1
>> 1
print b.prm5
>> 5

Open in new window

0
Comment
Question by:koleto115
  • 4
  • 2
6 Comments
 
LVL 41

Expert Comment

by:HonorGod
ID: 24205205
The definition of c2.__init__ needs to include a call to the base class constructor, and supply values for the expected parameters
class c2( c1 ) :
  def __init__( self, a4, a5 ) :
    c1.__init__( self, 0, 0, 0 )
    self.prm4 = a4
    self.prm5 = a5

Open in new window

0
 
LVL 41

Accepted Solution

by:
HonorGod earned 2000 total points
ID: 24205226
better though would be to require the base values to be specified...
class c2( c1 ) :
  def __init__( self, a1, a2, a3, a4, a5 ) :
    c1.__init__( self, a1, a2, a3 )
    self.prm4 = a4
    self.prm5 = a5

Open in new window

0
 

Author Comment

by:koleto115
ID: 24205403
Thanks!

I didn't know that I can access the super class constructor like this c1.__init__
Can I pass object instance instead?
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
LVL 41

Expert Comment

by:HonorGod
ID: 24205681
The code below might work for you.

Thanks for the grade & points.

Good luck & have a great day



>>> class c1 :
...   def __init__( self, a1, a2, a3 ) :
...     self.prm1 = a1
...     self.prm2 = a2
...     self.prm3 = a3
...
>>> class c2( c1 ) :
...   def __init__( self, c, a4, a5 ) :
...     c1.__init__( self, c.prm1, c.prm2, c.prm3 )
...     self.prm4 = a4
...     self.prm5 = a5
...
>>> a = c1( 1, 2, 3 )
>>> b = c2( a, 4, 5 )
>>>
>>> b.prm1
1
>>>

Open in new window

0
 

Author Comment

by:koleto115
ID: 24205703
Wow supper!
Thanks!
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 24206087
You're welcome.  Have fun
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
Suggested Courses

616 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question