On Thu, 12 Jan 2006, Christopher Spears 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? Hi Chris, Yes, that's it. __init__'s doesn't have to do with overloading, but does have to do with initialization. It fires off when an instance is being instantiated. For example: ###### class Dog: def __init__(self, name): self.name = name def bark(self): return "%s says 'bow wow'" % self.name ###### When we instantiate a Dog, we pass parameters that will be used to call __init__, and in effect, we "initialize" our dog: ### >>> d = Dog("Ein") >>> d.bark() "Ein says 'bow wow'" >>> k = Dog("Kujo") >>> k.bark() "Kujo says 'bow wow'" >>> Dog() Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: __init__() takes exactly 2 arguments (1 given) ### Note that in the third call to Dog, we see that if we don't pass enough arguments, that __init__() won't fire off properly. Does this make sense? _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor