python: passing parameters to new objects

is there a way to pass parameters when creating a new instance of an object.

I’ve had a look on the net for this and the only method I can think of is used for inheritance.



Please can you explain a little more?

Perhaps you mean passing of parameters when creating a new class instance?

If so, then its nice and simple.

class foo:
  # __init__ is the closest you will get to a constructor.
  # If nothing is passed, then val is set to 0 automagically.
  def __init__(self, val = 0):
    self.val = val

  def printVal(self):
    print self.val

# Create instance of class foo with no params
a = foo()

# Create instance of class foo passing 10 as param.
b = foo(10)

If thats not what your after, then try dive into python. You can get it here…

Have fun!

sorry yeah that was exactly what I was after I forgot that init was the constructor.

Thanks angain sandman.