23

I was looking at the source code to the hasattr built-in function and noticed a couple of lines that piqued my interest:

Py_INCREF(Py_False);
return Py_False;

...

Py_INCREF(Py_True);
return Py_True;

Aren't Py_False and Py_True global values? Just out of sheer curiosity, why is Python keeping a reference count for these variables?

8
  • 9
    @S.Lott since when to questions here have to relate to particular concrete programming problems. Commented Sep 22, 2009 at 14:44
  • 17
    I currently have the problem that I don't understand the need to increment the reference counts on Py_True and Py_False. Commented Sep 22, 2009 at 14:44
  • 14
    I find these types of questions the most interesting. Too many questions amount to, "How do I make this specific thing work?", and then the OP moves on to some other pressing concern. This question will actually lead to a deeper understanding of the system as a whole, and therefore better programming within that system. Commented Sep 22, 2009 at 15:28
  • 1
    @Chazadanga. LOL I think you're confused. Commented Jul 20, 2010 at 18:20
  • 1
    @Chazadanga, surely that comment was aimed at S.Lott, otherwise it makes no sense whatsoever. Commented Dec 2, 2014 at 16:35

2 Answers 2

23

It's to make all object handling uniform. If I'm writing C code that handles a return value from a function, I have to increment and decrement the reference count on that object. If the function returns me True, I don't want to have to check to see if it's one of those special objects to know whether to manipulate its reference count. I can treat all objects identically.

By treating True and False (and None, btw) the same as all other objects, the C code is much simpler throughout.

Sign up to request clarification or add additional context in comments.

6 Comments

Additionally: T F and None are actually objects. They just don't have any methods. docs.python.org/c-api/bool.html
they technically have methods. Every object has methods.
len(dir(True)) is 54 -- chock full of methods. They're just all special (double-underscores-named) ones.
Likely Unladen Swallow will change this. This is exactly the kind of things they are trying to optimize.
"Special cases aren't special enough to break the rules."
|
0

As of Python 3.12, CPython no longer alters the reference count on False and True. PEP 683 introduced the idea of an 'immortal' object, for which INCREF/DECREF is a no-op.

According to the glossary, runtime-created singletons such as True are included in the default set of immortal objects:

If an object is immortal, its reference count is never modified, and therefore it is never deallocated while the interpreter is running. For example, True and None are immortal in CPython.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.