Use of the "is" statement
Delaney, Timothy (Tim)
tdelaney at avaya.com
Sun Jun 29 20:06:27 EDT 2008
Maric Michaud wrote:
> Le Friday 27 June 2008 18:26:45 Christian Heimes, vous avez écrit :
>> Ask yourself if you are interested if f.tell() returns exactly the
>> same 0 object ("is") or a number that is equal to 0 ("==").
>
> That said, "f.tell() == 0" and "f.tell() != 0" should be written
> "f.tell()" and "not f.tell()" in python.
>
> if not f.tell() :
> print 'at the beginning of the file"
No, because semantically you're not using the result of f.tell() as a boolean value. tell() returns a *position*, and he is checking for a particular position.
If he'd instead been testing for f.tell() == 2, would you have advisd that he use "if f.tell()"?
Ignoring the triviality of the below code, would you suggest refactoring:
for i, v in enumerate(iterable):
if i == 0:
doSomethingSpecial()
else:
print i
as:
for i, v in enumerate(iterable):
if not i:
doSomethingSpecial()
else:
print -i
?
Tim Delaney
More information about the Python-list
mailing list