namespace issue?

Pierre Fortin pfortin at pfortin.com
Thu Jun 21 18:08:58 EDT 2001


Michael Powe wrote:
> 
> >>>>> "Christopher" == Christopher A Craig <com-nospam at ccraig.org> writes:
> 
>     Christopher> Michael Powe <michael+gnus at trollope.org> writes:
>     >> >>> def GetNum2(): ...  AC = raw_input("Area Code: ") ...  PN =
>     >> raw_input("Phone Number: ") ... if not PN : PN = '000-0000'
>     >> ... if not AC : AC = '503' ... if len(PN) < 8 and not
>     >> string.find(PN,'-'): ...  Ph = PN[:3] + '-' + PN[3:] ... return
>     >> Ph
> 
>     Christopher>   string.find(PN, '-') succeeds (since PN contains a
>     Christopher> '-'), so len(PN)<8 and not string.find(PN,'-')
>     Christopher> returns false, meaning that Ph = PN[:3] + '-' +
>     Christopher> PN[3:] is never executed, and Ph is not defined when
>     Christopher> the return statement is reached.
> 
> Actually, it doesn't contain the dash.  The dash is only inserted if I
> press the enter key, leaving the PN string empty.  In the case of an
> empty string, the default string of zeroes is put into the variable.
> But, the error persists even if I take that conditional out.  However,
> you pointed me in the right direction.  The 'not string.find(PN, '-')'
> part of the conditional is returning false when there is no dash in
> the string.  If I take that part of the conditional out, and just use
> the first part, it works as designed.
> 
> So now the question is, 'why'?  Am I misusing the string.find()
> function? Okay, string.find() returns -1 when the string is not found,
> which would seem to be what I expected, a false result.  Maybe python
> doesn't treat -1 as false?  Well, I'll try being explicit and testing
> for -1 return.  I probably have just confused myself hopelessly.

Sometimes -1 is used for failure; BUT -1 is NOT false...  in those cases, you
can simply add 1 to the return to force a failure to return 0 (false)...

HTH,
Pierre

> Thanks for the ... clue.  I saw a great line in an email today  -- "Is
> that the clue phone ringing?  I think it's for you."  And I think I'll
> go answer it.
> 
>     Christopher> So, yes, it is because of the conditional, but no it
>     Christopher> has nothing to do with namespaces.  (unlike in C or
>     Christopher> C++, in Python not all blocks get their own
>     Christopher> namespaces, only functions, modules, and classes (and
>     Christopher> probably something I forgot))
> 
>     Christopher> Also, yes, raw_input returns a string.
> 
> Okay, thanks.
> 
> mp
> 
> --
>                              Michael Powe
>               looie at aracnet.com     michael at trollope.org
>           "All we are basically are monkeys with car keys."
>                          -- Northern Exposure

-- 
Support Linux development:  http://www.linux-mandrake.com/donations/
Last reboot reason:  01/03/27: winter storm 6hr power outage



More information about the Python-list mailing list