943,518 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 5026
  • Python RSS
Oct 24th, 2007
0

super function

Expand Post »
Python Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
night_guard is offline Offline
3 posts
since Oct 2007
Oct 24th, 2007
0

Re: super function

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:

Python Syntax (Toggle Plain Text)
  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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Oct 26th, 2007
0

Re: super function

thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
night_guard is offline Offline
3 posts
since Oct 2007
Dec 30th, 2010
0
Re: super function
Python Syntax (Toggle Plain Text)
  1. class x(object):
  2. def __init__(self):
  3. self.x=1
  4.  
  5. class z(object):
  6. def __init__(self):
  7. self.z=1
  8.  
  9. class y(x,z):
  10. def __init__(self):
  11. super(y, self).__init__()

And what is it doing "super" function in here?
Reputation Points: 10
Solved Threads: 1
Newbie Poster
Scorpiusix is offline Offline
1 posts
since Dec 2010
Dec 30th, 2010
0
Re: super function
Click to Expand / Collapse  Quote originally posted by Scorpiusix ...
Python Syntax (Toggle Plain Text)
  1. class x(object):
  2. def __init__(self):
  3. self.x=1
  4.  
  5. class z(object):
  6. def __init__(self):
  7. self.z=1
  8.  
  9. class y(x,z):
  10. def __init__(self):
  11. super(y, self).__init__()

And what is it doing "super" function in here?
It calls x.__init__():
python Syntax (Toggle Plain Text)
  1. class x(object):
  2. def __init__(self):
  3. print ("x.__init__()")
  4. self.x=1
  5.  
  6. class z(object):
  7. def __init__(self):
  8. print ("z.__init__()")
  9. self.z=1
  10.  
  11. class y(x,z):
  12. def __init__(self):
  13. super(y, self).__init__()
  14.  
  15. y()
  16.  
  17. """my output -->
  18. x.__init__()
  19. """
Super is not very useful. Call x.__init__() directly.
Reputation Points: 930
Solved Threads: 666
Posting Maven
Gribouillis is offline Offline
2,655 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Telnetlib help
Next Thread in Python Forum Timeline: CRC-24





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC