Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Watchpoints to monitor variables in Python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to use Watchpoints to monitor variables in Python, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

Watchpoints is a simple but powerful tool that helps you monitor variables when debugging Python.

When debugging code, you are often faced with figuring out when a variable changes. If you don't have any advanced tools, you can choose to use print statements to output variables when you expect them to change. However, this is a very inefficient method, because variables can change in many places, and constantly printing them to the terminal can cause a lot of interference, while printing them to a log file becomes troublesome.

This is a common problem, but now there is a simple but powerful tool that can help you monitor variables: watchpoints.

The concept of "watchpoint" is common in C and C++ debuggers to monitor memory, but there is a lack of tools in Python. Watchpoints fills this gap.

Installation

To use it, you must first install it with pip:

$python3-m pip install watchpoints uses watchpoints in Python

Use the watch function to monitor any variable you want to monitor.

From watchpoints import watch a = 0watch (a) a = 1

When a variable changes, its value is printed to standard output:

= Watchpoints Triggered = Call Stack (most recent call last): (my_script.py:5): > a = 1a most recent call last 0-> 1

The information includes:

The line in which the variable is changed.

Call stack.

The previous / current value of the variable.

It applies not only to variables themselves, but also to changes in objects:

From watchpoints import watch a = [] watch (a) a = {} # trigger a ["a"] = 2 # trigger

When the variable an is reassigned, the callback is triggered, and when the object assigned to a changes.

More interestingly, monitoring is not scoped. You can observe variables / objects anywhere, and callbacks will be triggered no matter what function the program is executing.

From watchpoints import watch def func (var): var ["a"] = 1 a = {} watch (a) func (a)

For example, this code prints out:

= Watchpoints Triggered = Call Stack (most recent call last): (my_script.py:8): > func (a) func (my_script.py:4): > var ["a"] = 1a: {}-> {'averse: 1}

The watch function can monitor not only a variable, but also the attributes and elements of a dictionary or list.

From watchpoints import watch class MyObj: def _ init__ (self): self.a = 0 obj = MyObj () d = {"a": 0} watch (obj.a, d ["a"]) # Yes, you can do this obj.a = 1 # trigger d ["a"] = 1 # trigger

This can help you narrow down to specific people you are interested in.

If you are not satisfied with the output format, you can customize it. Just define your own callback function:

Watch (a, callback=my_callback) # or global setting watch.config (callback=my_callback)

When triggered, you can even use pdb:

Watch.config (pdb=True)

This is similar to the behavior of breakpoint () and will give you a debugger-like experience.

If you don't want to import this function in every file, you can make it global through the install function:

Watch.install () # or watch.install ("func_name"), and then use it as func_name ()

Personally, I think the coolest thing about watchpoints is its intuitive use. Are you interested in some data? As long as you "observe" it, you will know when your variables change.

Try watchpoints

I developed and maintained watchpoints on GitHub, released it under the Apache 2.0 license, installed it, and used it.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report