help on ultra newbie program please

Steve Holden sholden at holdenweb.com
Thu May 22 16:06:27 EDT 2003


"Andrew Walkingshaw" <andrew-usenet at lexical.org.uk> wrote in message
news:slrnbcnhnd.183.andrew-usenet at athena.jcn.srcf.net...
> In article <3ECBC0CD.AB706DBF at engcorp.com>, Peter Hansen wrote:
> > Andrew Walkingshaw wrote:
> >>
> >> # set password to be blank initially
> >> password = ""
> >> # and create a variable to count the number of failed login attempts
> >> numfailures = 0
> >>
> >> while numfailures < 3:
> >>     password = raw_input("Password: ")
> >>     if password == "unicorn":
> >>         # success, so get out of the loop
> >>         break
> >>     else:
> >>         # increment number of attempts by one
> >>         numfailures = numfailures + 1
> >
> >
> > Extremely minor nit: setting password to "" in the first
> > line above is useless in this case, as it will always be
> > set to the user input after the call to raw_input()...
>
> Of course; however, I don't think it does any real harm (OK, it's very
> slightly memory inefficient.)
>
> I tend to set default values for any variable I'm going to use in
> serious code: I've been bitten by subtle bugs ("surely that's the first
> time I've used x in this scope...") too many times not to. :)
>

That seems a bit strange (though I cannot claim to b e the universal
arbitrator of what's strange and what's not). You seem to be saying that, to
avoid AttributeError exceptions due to not having initialized your variables
before you use them, you initialize them at the start of your code.

Surely that would make your code even *more* wrong in some cases: the
AttributeError is actually a very useful indication that there *is* an error
in your code, whereas the silent absence of failure masks what's really a
logic error.

Unless I'm misunderstanding you somehow, I believe that's taking defensive
programming just a little too far.

regards
--
Steve Holden                                  http://www.holdenweb.com/
Python Web Programming                 http://pydish.holdenweb.com/pwp/







More information about the Python-list mailing list