avatarLaxfed Paulacy

Summary

This web content provides an explanation of how to inspect Python's dunder objects, specifically focusing on variable storage and nonlocal scope management within functions.

Abstract

The article titled "PYTHON — Inspecting Dunder Objects in Python" delves into the intricacies of Python's internal variable management and scope tracking mechanisms. It introduces key concepts such as the .__code__ object, .co_cellvars, .co_freevars, .__closure__, Cell, and .cell_contents attributes, which are essential for understanding how Python handles variables and function scopes. Through a detailed code example, the tutorial demonstrates how to inspect these dunder objects to access and modify variables in the nonlocal scope, even after the original function has finished execution. The article emphasizes that while direct manipulation of dunder objects is generally intended for Python's internal use and not recommended for casual users, having this knowledge can be invaluable for advanced debugging and gaining a deeper understanding of Python's internal workings.

Opinions

  • The article suggests that understanding dunder objects is crucial for a comprehensive grasp of Python's variable and scope management.
  • It

PYTHON — Inspecting Dunder Objects in Python

Information technology and business are becoming inextricably interwoven. I don’t think anybody can talk meaningfully about one without the talking about the other. — Bill Gates

PYTHON — Summary of Python’s defaultdict

# Inspecting Dunder Objects in Python

In this tutorial, we will delve into inspecting dunder objects in Python. We will explore where Python stores variables and how it keeps track of the nonlocal scope of a function, even after the function’s scope has been dereferenced.

First, let’s start by understanding the vocabulary that will be used in this tutorial:

  1. .__code__ object
  2. .co_cellvars attribute
  3. .co_freevars attribute
  4. .__closure__ object
  5. Cell object
  6. .cell_contents attribute

Now, let’s dive into the code and inspect these dunder objects using Python code snippets.

def outer():
    message = "world"
    def inner():
        print(message)
    return inner

inner_returned = outer()

# Inspecting co_cellvars in the __code__ object of the outer function
outer_code_object = outer.__code__
print(outer_code_object.co_cellvars)  # ('message',)

# Inspecting __closure__ of the inner_returned function object
inner_returned_closure = inner_returned.__closure__
print(inner_returned_closure)  # (<cell at 0x000001E4F209B400: str object at 0x000001E4F1FEDF80>,)

# Accessing the value of the cell_contents
message_value = inner_returned_closure[0].cell_contents
print(message_value)  # 'world'

# Changing the value of the variable in the nonlocal scope
inner_returned.__closure__[0].cell_contents = "oops"
print(inner_returned())  # oops

In the code snippet above, we inspect the .__code__ object of the outer() function to see where the message variable is stored. We also inspect the .__closure__ of the inner_returned function object to access the value of the message variable and change its value in the nonlocal scope.

It’s important to note that modifying dunder objects is typically for internal use of Python and is not recommended for end users. However, Python provides the flexibility to access and modify these objects if necessary.

By understanding and inspecting dunder objects, you gain insight into how Python manages variables and scopes, which can be valuable for advanced debugging and understanding the inner workings of Python.

In conclusion, inspecting dunder objects in Python provides a deeper understanding of variable storage and scope management, allowing you to gain insights into the internal mechanisms of Python.

PYTHON — File System Summary in Python

ChatGPT
Dunder
Objects
Python
Inspecting
Recommended from ReadMedium