|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
__init__()
Hi,
I'm going thru' a big piece of code and trying to understand it... here is an example of it... Code:
>>> class calci(no): ... def __init__(self,n,msg): ... no.__init__(self,n,msg) ... Can't figure out what this __init__ does here.... the no.__init__(self,n,msg) confuses me. If anyone cld explain that to me? Thanks & Rgds, Subha ![]() |
|
#2
|
||||
|
||||
|
There appears to be a class called no.
The calci class inherits all it's methods If you redefine a method e.g. __init__ you can still get access to the undelying definition as you see in the sample. In the no class there is also a defined __init__ method that is being called to ensure the object is fully initialized. Only by exploring the no doc and code will you know what is going on there and if the call is really needed. grim ![]()
__________________
*** Experimental Python Markup CGI V2 *** |
|
#3
|
||||
|
||||
|
Quote:
It is calling the constructor method of the super class (i.e.) no and passing it the same arguments it was passed. You would normally use this when you're creating a class that does everything that the base class does and some more. These days, if you declare the base object to be of type "object", then you can use the new super() keyword as well. Code:
#!/usr/bin/env python
class BaseClass(object):
def __init__(self, msg):
print "Class BaseClass says: " + msg
class DerivedClass(BaseClass):
def __init__(self, msg):
print "Class DerivedClass says " + msg
super(DerivedClass, self).__init__(msg)
# instead of BaseClass.__init__(self, msg)
x = DerivedClass("foo")
__________________
Up the Irons What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home. "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest Down with Sharon Osbourne Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > __init__() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|