1

For the given code

def greater(n):
    if n > 3:
        res = True
    else:
        res = False

    return res

a = greater(5)
print(hex(id(a)))
print(hex(id(True)))

b = True
print(hex(id(b)))

if  a == True:
    print('yes')
else:
    print('no')

pylint suggests pylint_example.py:16:4: C0121: Comparison 'a == True' should be 'a is True' if checking for the singleton value True, or 'a' if testing for truthiness (singleton-comparison)

a is True will check both address and value and I cannot assume immutable variables will have the same address

Thus, changing a == True to a is True may lead to incorrect results (a and True may have different addresses in memory). Why does pylint suggest that?

Though

print(hex(id(a)))
print(hex(id(True)))

b = True
print(hex(id(b)))

part gives consistent results. I am not sure if that would work in general.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Chungji
  • 59
  • 5
  • 1
    Does this answer your question? [Boolean identity == True vs is True](https://stackoverflow.com/questions/27276610/boolean-identity-true-vs-is-true) – Dash Nov 26 '22 at 08:17
  • 1
    Your whole function should be `return n > 3`. Everything else is unnecessary. – Klaus D. Nov 26 '22 at 08:20
  • Not sure. I am keep on asking in the comment of answers. Nephanth's answer is closest. I am testing py39 – Chungji Nov 26 '22 at 08:34

2 Answers2

2

True and False are unique singletons, not immutable. If a has the value True, then a and True do have the same memory address.

Source: PEP-0285 and In Python are the built in constants True and False unique?

Dash
  • 1,191
  • 7
  • 19
  • Thanks. Is there any source can confirm the unique entry will have the unique address in memory? – Chungji Nov 26 '22 at 08:28
  • @Chungji what do you mean that they will have unique addresses in memory? – Dash Nov 26 '22 at 08:31
  • Yes. That is what I would like to know – Chungji Nov 26 '22 at 08:33
  • @Chungji `True` is a singleton, so *all* references to `True` point to the same object/memory address. I don't see how "unique memory addresses" is related. – Dash Nov 26 '22 at 08:38
  • Thank you so much. Is all reference to singleton to the same memory address in https://docs.python.org? Where can I find it? – Chungji Nov 26 '22 at 08:42
  • @Chungji the closest I can quickly point you to is [`bool()`](https://docs.python.org/3/library/functions.html#bool), which guarantees they are the only instances of `bool`, saying "The `bool` class is a subclass of `int` (see Numeric Types — int, float, complex). It cannot be subclassed further. Its only instances are `False` and `True`." – Dash Nov 26 '22 at 08:45
0

PEP 8 claims that correct way is to use if variable giving following example

if greeting:

and claims that

if greeting == True:

is wrong and

if greeting is True:

is worse.

Daweo
  • 31,313
  • 3
  • 12
  • 25
  • To my limited knowledge, PEP8 is a style guide, rather than in-principle right/wrong. What is the difference between `if greeting:` and `if greeting == True:`? Sorry for my stupid understanding. – Chungji Nov 26 '22 at 08:36
  • if greeting:: This condition checks the truthiness of the greeting variable. If greeting is any non-empty value, the condition if greeting: will evaluate to True. On the other hand, if greeting is an empty string, 0, None, or an empty container, the condition will evaluate to False. if greeting == True:: This condition explicitly checks if the value of greeting is exactly equal to True. It compares greeting with the boolean value True using the equality operator ==. It will evaluate to True only if greeting holds the value True itself. – Naveen Gabriel Jun 28 '23 at 16:21