From df99f75902ff0e859e4894607702d82b21549742 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Mon, 13 Jul 2009 21:52:08 -0400 Subject: [PATCH] clarified what we mean by constructor --- iterators.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iterators.html b/iterators.html index fb4e56f..0f7795c 100755 --- a/iterators.html +++ b/iterators.html @@ -82,7 +82,7 @@ body{counter-reset:h1 6} def __init__(self, max):
  1. Classes can (and should) have docstrings too, just like modules and functions. -
  2. The __init__() method is called immediately after an instance of the class is created. It would be tempting but incorrect to call this the constructor of the class. It’s tempting, because it looks like a constructor (by convention, the __init__() method is the first method defined for the class), acts like one (it’s the first piece of code executed in a newly created instance of the class), and even sounds like one. Incorrect, because the object has already been constructed by the time the __init__() method is called, and you already have a valid reference to the new instance of the class. +
  3. The __init__() method is called immediately after an instance of the class is created. It would be tempting — but technically incorrect — to call this the “constructor” of the class. It’s tempting, because it looks like a C++ constructor (by convention, the __init__() method is the first method defined for the class), acts like one (it’s the first piece of code executed in a newly created instance of the class), and even sounds like one. Incorrect, because the object has already been constructed by the time the __init__() method is called, and you already have a valid reference to the new instance of the class.

The first argument of every class method, including the __init__() method, is always a reference to the current instance of the class. By convention, this argument is named self. This argument fills the role of the reserved word this in C++ or Java, but self is not a reserved word in Python, merely a naming convention. Nonetheless, please don’t call it anything but self; this is a very strong convention.