Not x.islower() has different output than x.isupper() in list output...

Chris Angelico rosuav at gmail.com
Fri Apr 29 22:10:50 EDT 2016


On Sat, Apr 30, 2016 at 11:55 AM, Christopher Reimer
<christopher_reimer at icloud.com> wrote:
> On 4/29/2016 6:29 PM, Stephen Hansen wrote:
>>
>> If isupper/islower were perfect opposites of each-other, there'd be no
>> need for both. But since characters can be upper, lower, or *neither*, you
>> run into this situation.
>
>
> Based upon the official documentation, I was expecting perfect opposites.
>
> str.islower(): "Return true if all cased characters [4] in the string are
> lowercase and there is at least one cased character, false otherwise."
>
> https://docs.python.org/3/library/stdtypes.html?highlight=islower#str.islower
>
> str.isupper(): "Return true if all cased characters [4] in the string are
> uppercase and there is at least one cased character, false otherwise."
>
> https://docs.python.org/3/library/stdtypes.html?highlight=isupper#str.isupper
>
> Here's the footnote that may or not be relevant to this discussion: "[4]
> Cased characters are those with general category property being one of “Lu”
> (Letter, uppercase), “Ll” (Letter, lowercase), or “Lt” (Letter, titlecase)."
>
> A bug in the docs?

>>> def case(ch):
...     return (
...         ("Lower " if ch.islower() else "") +
...         ("Upper " if ch.isupper() else "") +
...         unicodedata.category(ch)
...     )

Both functions require that there be at least one cased character:

>>> case("@")
'Po'

And then each one requires a specific character class:

>>> case("a")
'Lower Ll'
>>> case("A")
'Upper Lu'

Other classes don't count:

>>> case("\u01C5")
'Lt'

There are three categories of "cased characters". Both functions check
for exactly one of those categories. Thus they are not opposites.

ChrisA



More information about the Python-list mailing list