Re: [Tutor] != -1: versus == 1

2009-07-17 Thread Bill Campbell
On Fri, Jul 17, 2009, pedro wrote:
 Hi I have been trying to understand a python script and I keep coming  
 across this kind of structure
 that says If it is not equal to negative one

 
 for line in theLines:
if line.find(Source Height) != -1:
 #etc...
 ###

 Is there some special reason for this. Why not just write If it is  
 equal to one

The string find method returns -1 if the string is not found,
otherwise it returns the 0-based index into the string matching
the argument to find.  The test above will return -1 if ``Source
Heigtht'' is not in line, and one generally wants to have the
test return True if there is something to do.  The alternative
would be to say ``if not line.find('Source Height') == -1: ...''

Bill
-- 
INTERNET:   b...@celestial.com  Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/  PO Box 820; 6641 E. Mercer Way
Voice:  (206) 236-1676  Mercer Island, WA 98040-0820
Fax:(206) 232-9186  Skype: jwccsllc (206) 855-5792

Windows is a computer virus with a user interface!!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] != -1: versus == 1

2009-07-17 Thread Sander Sweers
2009/7/17 pedro pedrooconn...@gmail.com:
 
 for line in theLines:
   if line.find(Source Height) != -1:
 #etc...
 ###

 Is there some special reason for this. Why not just write If it is equal to
 one

Yes, str.find() returns -1 on failure. See below the documentation for
str.find()

 |  find(...)
 |  S.find(sub [,start [,end]]) - int
 |
 |  Return the lowest index in S where substring sub is found,
 |  such that sub is contained within s[start:end].  Optional
 |  arguments start and end are interpreted as in slice notation.
 |
 |  Return -1 on failure.

Idle example:

 teststr = 'a'
 teststr.find('a')
0
 teststr.find('b')
-1

Greets
Sander
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] != -1: versus == 1

2009-07-17 Thread Lie Ryan
pedro wrote:
 Hi I have been trying to understand a python script and I keep coming
 across this kind of structure
 that says If it is not equal to negative one
 
 
 for line in theLines:
if line.find(Source Height) != -1:
 #etc...
 ###
 
 Is there some special reason for this. Why not just write If it is
 equal to one
 
 #
 for line in theLines:
if line.find(Source Height) == 1:
 #etc...
 ###

Nothing special, it just they have different meaning (and different
results). The former (!= -1) tests whether the str.find() method does
not fail finding Source Height (i.e. str.find() succeeds finding
Source Height) while the latter tests whether Source Height is in
the second position in the string.

But if it were me, I'd write the former (!= -1) with 'in' operator:

for line in theLines:
if Source Height in line:
...

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] != -1: versus == 1

2009-07-17 Thread pedro
Thanks for the replies. I think my mistake was assuming that 1 meant 
true when if fact it means index 1. Whe I tested for == 1, since 
Source Height was coincidentally at index 1 it returned something 
which looked like it worked. Thanks for the clarification. And thanks 
for the suggestion to use in that is much more readable.

Pete


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor