I have tried and errored a reasonable amount of times
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Sun Aug 31 04:37:04 EDT 2014
For future reference, here is a hint as to how to debug problems like this,
and a cleaner way to write the code.
Seymore4Head wrote:
> On Sat, 30 Aug 2014 13:48:09 -0500, Tim Chase
> <python.list at tim.thechases.com> wrote:
>>> if e[0].isupper == False:
>>> print ("False")
>>> if e[0].isupper == True:
>>> print ("True")
[...]
>>There's a difference between e[0].isupper which refers to the method
>>itself, and e[0].isupper() which then calls that method. Call the
>>method, and you should be good to go.
>
> That works.
> Thanks
We've all done it. I think that anyone who tells you they've never forgotten
the brackets on a zero-argument function call is probably lying :-) And the
worst thing is that you can stare and stare and stare at the code for a
week and not see what's wrong.
Here are two suggestions for debugging difficult problems like this:
- Take a break, and look at it with fresh eyes. E.g. work on something
else for an hour or three, or go and have lunch.
- Use print() to see the intermediate results:
a = e[0].isupper
print(e[0], a, a == False, a == True)
You expect to see something like:
T True False True
but when you see:
T <built-in method isupper of str object at 0xb7d44820> False False
at least you will know *what* is going on, even if you're unsure of *why*.
Now, here is a better way to write your code. Since isupper() returns True
or False, there is no need to compare to True or False to get a boolean
value. It's already a boolean value! So we can write:
if not e[0].isupper():
print("False")
if e[0].isupper():
print("True")
or better still:
if e[0].isupper():
print("initial letter is upper case")
else:
print("initial letter is lower case")
(More descriptive messages are better than cryptic messages.)
--
Steven
More information about the Python-list
mailing list