Internals of Python 3.x
eBook - ePub

Internals of Python 3.x

Derive Maximum Code Performance and Delve Further into Iterations, Objects, GIL, Memory management, and various Internals

  1. English
  2. ePUB (mobile friendly)
  3. Available on iOS & Android
eBook - ePub

Internals of Python 3.x

Derive Maximum Code Performance and Delve Further into Iterations, Objects, GIL, Memory management, and various Internals

About this book

Deroute the syntactical way and start exploring the language from the source

Key Features
? In-depth practical understanding of CPython's internal workings.
? Step-by-step source code walkthrough utilizing descriptors on source code lines.
? Cutting-edge coverage of the interpreter, GIL, compilation, and memory allocations to help you develop better systems.

Description
Internals of Python 3.x transform a programmer's learning path by emphasizing the source code over the syntax to teach things from the ground up in nearly the same amount of time and effort.The book delves into the structure and distinctions between the primary Python object and iterable objects. The iterable types, namely, lists and tuples, have been thoroughly defined in the structure and operations. The internals of sets and dictionaries, which are data structures that provide O(1) insertion and search, have been thoroughly discussed. Memory allocation explains how Python handles memory for tiny and large objects. The chapter on GIL explains how the GIL works, which is halted by a semaphore and a conditional variable. The chapter on Async Python describes how the async module generates coroutines and async functions that can be executed on an event loop and interact through events.After reading this book, you will be more confident to create high-performance code on a day-to-day basis.

What you will learn
? Utilize data structures effectively for a variety of application functions.
? Discover how to optimize Python code performance.
? Develop an understanding of memory optimization and how to design programs accordingly.
? Investigate the inner working of GIL and Interpreter in detail.
? Recognize the internals of the garbage collection and reference counting processes.

Who this book is for
This book is intended for Python practitioners, new coding aspirants, and experienced Python developers who want to construct their frameworks and libraries by investigating tokenizers, parsers, code compilers, interpreters, memory management, GIL, and garbage collection. Prior programming skills in C may help you get the most out of this book.

Table of Contents
1. Design of Generic Objects
2. Basic Python Types
3. Iterable Sequence Objects
4. Set and Dictionary
5. Functions and Generators
6. Memory Management
7. Interpreter and Opcodes
8. GIL and Multithreading
9. Async Python
10. Source Code Layout and the Compiler Stages

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription.
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn more here.
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Yes! You can use the Perlego app on both iOS or Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app.
Yes, you can access Internals of Python 3.x by Prashanth Raghu in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Python. We have over one million books available in our catalogue for you to explore.

CHAPTER 1

Design of Generic Objects

The components of a generic Python object contain the memory layout, operations, and memory management. This chapter covers the similarities and differences between Python types. The chapter begins with the PyObject and explains how it encapsulates the type, reference count, and bookkeeping pointers. The PyVarObject, which is a PyObject encapsulation with the size of the data structure, is covered along with the different functional attributes of the type such as numerical, sub stringing, and so on.

Structure

In this chapter, we will cover the following topics:
  • The PyObject
    • Understanding _PyObject_HEAD_EXTRA
    • Reference counting
  • The PyVarObject
  • The PyTypeObject
    • Generic type function prototypes
    • Specific type function prototypes
    • The Type object substructures
      • The PyNumberMethods substructure
      • The PySequenceMethods substructure
      • The PyMappingMethods substructure
  • The type object
    • Name and sizes of types
    • Allocator, deallocator, and initialization functions
    • Iterator functions
    • Methods and attributes

Objective

After studying this chapter, you will be able to understand the basic components, the object and the type, which compromise both the data types and data structures. You will also learn about the TypeObject that contains the operations implemented in the data types.

The PyObject

The PyObject contains the details of all Python objects and understanding its structure is crucial before we delve into the implementation of reference counting and Python types, which are key elements of this generic object:
Include/object.h Line no 104
typedef struct _object {
_PyObject_HEAD_EXTRA
Py_ssize_t ob_refcnt;
struct _typeobject *ob_type;
} PyObject;
The PyObject contains three elements, which are as follows:
  • _PyObject_HEAD_EXTRA: A macro that expands to track all objects in the Python heap in debug mode.
  • ob_refcnt: An integer value that holds the reference count of the object.
  • _typeobject: A pointer to the type of the object such as integer/list/dictionary, which contains the operations that can be performed on an instance of the type.
The upcoming chapters contain both the implementation and structure of each of these elements.

Understanding _PyObject_HEAD_EXTRA

The _PyObject_HEAD_EXTRA adds a forward and reverse pointer to every object used to construct a doubly linked list that tracks live objects in the Python heap at runtime. This flag has to be enabled when building the Python executable using the following command:
../configure CFLAGS='-DPy_DEBUG -DPy_TRACE_REFS' --with-pydebug
The code block explaining the definition of the _PyObject_HEAD_EXTRA macro is as follows:
Include/object.h Line no 67
#ifdef Py_TRACE_REFS -> 1
/* Define pointers to support a doubly-linked list of all live heap objects. */
#define _PyObject_HEAD_EXTRA \
struct _object *_ob_next; -> 2 \
struct _object *_ob_prev; -> 3
#define _PyObject_EXTRA_INIT 0, 0,
#else
#define _PyObject_HEAD_EXTRA
#define _PyObject_EXTRA_INIT
#endif
Code insights are as follows:
  1. The _PyObject_HEAD_EXTRA adds the pointers only when Python is compiled with the Py_TRACE_REFS flag enabled.
  2. The _ob_next pointer points to the next created object in the linked list.
  3. The _ob_prev pointer points to the previously created object in the linked list.
The following code block explains the construction of the live object heap as a doubly-linked list:
Objects/object.c (Line no 81)
static PyObject refchain = {&refchain, &refchain};
void _Py_AddToAllObjects(PyObject *op, int force) {
….
if (force || op->_ob_prev == NULL) {
op->_ob_next = refchain._ob_next;
op->_ob_prev = &refchain;
refchain._ob_next->_ob_prev = op;
refchain._ob_next = op;
}
…..
}
The highlighted part explains the construction of the doubly-linked list with reference to the current pointer to the end of the list that is pointed to by refchain.
This reference chain can be accessed using the getobjects method of the sys module. The sample Python code demonstrates how the objects can be fetched using the method:
>>> import sys
>>> objs = sys.getobjects(1) # 1 returns the first object from the linked list.
>>> more_objs = sys.getobjects(20) # Increase the count to return more objects.
>>> type_objs = sys.getobjects(20, str) # A second argument
The following code ...

Table of contents

  1. Cover Page
  2. Title Page
  3. Copyright Page
  4. Dedication Page
  5. About the Author
  6. About the Reviewer
  7. Acknowledgement
  8. Preface
  9. Errata
  10. Table of Contents
  11. 1. Design of Generic Objects
  12. 2. Basic Python Types
  13. 3. Iterable Sequence Objects
  14. 4. Set and Dictionary
  15. 5. Functions and Generators
  16. 6. Memory Management
  17. 7. Interpreter and Opcodes
  18. 8. GIL and Multithreading
  19. 9. Async Python
  20. 10. Source Code Layout and the Compiler Stages
  21. Index