[Tutor] defining __init__

John Fouhy john at fouhy.net
Fri Jan 13 00:16:02 CET 2006


On 13/01/06, Christopher Spears <cspears2002 at yahoo.com> wrote:
> I just read about __init__ in the chapter on operator
> overloading in Learning Python.  What is __init__
> exactly?  Is it a special function that you can use to
> initialize attributes in an object upon creation?

Pretty much ...  Have you read about defining your own classes yet? [I
don't have Learning Python]

If you haven't, then don't worry about it --- the book will surely
cover __init__ when you get to defining classes.

But, either way, here is a short example:

>>> class MyClass(object):
...  def __init__(self):
...   print 'Initialising MyClass instance now...'
...   self.x = 3
...
>>> m = MyClass()
Initialising MyClass instance now...
>>> m.x
3

--
John.


More information about the Tutor mailing list