Thread: __init__()

    #1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Bombay, India
    Posts
    159
    Rep Power
    12

    __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. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    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
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Originally Posted by NewPythoner
    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.
    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

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo