Instructions in book don't work
Mike C. Fletcher
mcfletch at rogers.com
Thu Jan 10 02:11:09 EST 2002
The initialiser is indicated with a special name and must be exactly the
following name "__init__". That is, it is two "_" characters, followed
by "init", followed by two "_" characters.
You only have one "_" character before and after, so you haven't created
an __init__ method, but an _init_ method. Since there is a default
"__init__" method present for all classes, that is getting used, and
it's definition is something like this:
def __init__( self ):
pass
So the error is saying: sorry, the default __init__ method takes no
arguments.
Hope this helps,
Mike
Dan Hamm wrote:
> This is my very first attempt at using a newsgroup, so please bear with
> me. I am totally new to programming, and picked up the book "Lean to
> Program Using Python" by Alan Gauld. The problem I am trying to solve is
> this:
>
>
>
> On page 51 the author shows demonstration code for setting up a class -
>
>
>
> >>> class Address:
> ... def _init_(self, Hs, St, Town, Zip):
> ... self.Hs_Number = Hs
> ... self.Street = St
> ... self.Town = Town
> ... self.Zip_Code = Zip
>
>
>
> Then follows it up with the instructions to type in the following -
>
>
>
> >>> addr = Address(7,"High St.","Anytown","12345")
>
> >>> print addr.Hs_Number, addr.Street
>
>
>
> My problem is when I type the first line of the second set of
> instructions (addr = Address(7, ...) I get the following error -
>
>
>
> Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
> TypeError: this constructor takes no arguments
>
>
>
> I've checked to make sure I copied exactly what was in the book, but it
> just doesn't seem to work. Can anyone tell me where I went wrong?
>
--
_______________________________________
Mike C. Fletcher
http://members.rogers.com/mcfletch/
More information about the Python-list
mailing list