Skip to content Skip to sidebar Skip to footer

Derived Class Not Running Code In _init_

Why would the following code not print 'Hello'? # C derives from B, which derives from A, which derives from object class D(C): def _init_(self, *args, **kw): print 'Hello'

Solution 1:

You need to use double underscores:

def__init__(self, *args, **kw):

The method _init_ has no special meaning to Python and won't be called when instantiating.

Solution 2:

# C derives fromB, which derives fromA, which derives fromobject
class D(C):
  def __init__(self, *args, **kw):
    print "Hello"


foo = D('some_text')

double underscores.

Post a Comment for "Derived Class Not Running Code In _init_"