super function

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 3
Reputation: night_guard is an unknown quantity at this point 
Solved Threads: 0
night_guard night_guard is offline Offline
Newbie Poster

super function

 
0
  #1
Oct 24th, 2007
  1. class x(object):
  2. def __init__(self):
  3. self.x=1
  4.  
  5. class y(x):
  6. def __init__(self):
  7. super(y, self).__init__()
what is it doing "super" function in here?
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: super function

 
0
  #2
Oct 24th, 2007
The super() function allows you to access methods in the parent x that are shadowed by methods of the same name in the child y.

In this case, y.__init__ wants to call x.__init__. But the problem is that y.__init__ clobbers __init__ in the namespace; that is, x.__init__ doesn't get inherited. SO, super(y, self) returns x, so that your code means this:

  1. class x(object):
  2. def __init__(self):
  3. self.x=1
  4.  
  5. class y(x):
  6. def __init__(self):
  7. x.__init__(self)

Jeff
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 3
Reputation: night_guard is an unknown quantity at this point 
Solved Threads: 0
night_guard night_guard is offline Offline
Newbie Poster

Re: super function

 
0
  #3
Oct 26th, 2007
thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC