Python借助with语句实现代码段只执行有限次

作者:SDFDSJFJ 时间:2022-08-07 15:52:29 

debug的时候,有时希望打印某些东西,但是如果代码段刚好在一个循环或者是其他会被执行很多次的部分,那么用来print的语句也会被执行很多次,看起来就不美观。

例如:

a = 0
for i in range(3):
    a += 1
print(a)

这里在中间希望确认一下a的类型,debug的时候改成:

a = 0
for i in range(3):
    print(type(a))
    a += 1
print(a)
''' 打印结果:
<class 'int'>
<class 'int'>
<class 'int'>
3
'''

有3个 <class &lsquo;int&rsquo;>,很不好看。

为了解决这个问题,可以借助with语句实现,首先要定义一个能够在with语句中使用的类(实现了__enter__和__exit__):

from typing import Any

class LimitedRun(object):
    run_dict = {}

    def __init__(self,
                 tag: Any = 'default',
                 limit: int = 1):
        self.tag = tag
        self.limit = limit

    def __enter__(self):
        if self.tag in LimitedRun.run_dict.keys():
            LimitedRun.run_dict[self.tag] += 1
        else:
            LimitedRun.run_dict[self.tag] = 1
        return LimitedRun.run_dict[self.tag] <= self.limit

    def __exit__(self, exc_type, exc_value, traceback):
        return

tag是标签,相同标签共用执行次数计数器;limit是限制执行的次数。例子如下:

a = 0
for i in range(3):
    with LimitedRun('print_1', 1) as limited_run:
        if limited_run:
            print(type(a))
    a += 1
print(a)

打印结果:

<class 'int'>
3

a = 0
for i in range(3):
    with LimitedRun('print_1', 4) as limited_run:
        if limited_run:
            print(1, type(a))
    a += 1
for i in range(3):
    with LimitedRun('print_1', 4) as limited_run:
        if limited_run:
            print(2, type(a))
    a += 1
print(a)

 打印结果:(相同tag共用了计数器,因此总共只会执行4次)

1 <class 'int'>
1 <class 'int'>
1 <class 'int'>
2 <class 'int'>
6

a = 0
for i in range(3):
    with LimitedRun('print_1', 4) as limited_run:
        if limited_run:
            print(1, type(a))
    a += 1
for i in range(3):
    with LimitedRun('print_2', 4) as limited_run:
        if limited_run:
            print(2, type(a))
    a += 1
print(a)

打印结果:(不同tag不共用计数器)

1 <class 'int'>
1 <class 'int'>
1 <class 'int'>
2 <class 'int'>
2 <class 'int'>
2 <class 'int'>
6

来源:https://blog.csdn.net/qq_44980390/article/details/123673310

标签:Python,with,语句,代码,执行,有限次
0
投稿

猜你喜欢

  • 通过Turtle库在Python中绘制一个鼠年福鼠

    2021-03-01 03:48:12
  • 解析一个通过添加本地分区索引提高SQL性能的案例

    2023-07-22 13:29:32
  • python 中文乱码问题深入分析

    2023-06-12 19:04:37
  • Python Unittest自动化单元测试框架详解

    2023-09-13 23:13:56
  • Python如何实现远程方法调用

    2022-11-11 20:42:15
  • python实现字典(dict)和字符串(string)的相互转换方法

    2021-10-19 18:22:44
  • python基础教程之Hello World!

    2021-03-05 22:45:06
  • 网站LOGO设计规范的思考--2.网络LOGO的设计

    2007-10-14 11:02:00
  • Python基础之文件操作及光标移动详解

    2022-12-01 02:23:17
  • firefox扩展插件制作方法

    2007-10-12 13:50:00
  • tensorflow将图片保存为tfrecord和tfrecord的读取方式

    2022-01-22 11:32:04
  • CSS框架的利与弊

    2007-12-06 12:59:00
  • 寻找Dreamweaver鲜为人知的小秘诀

    2008-04-28 12:10:00
  • SQL语句练习实例之五 WMS系统中的关于LIFO或FIFO的问题分析

    2011-11-03 16:59:59
  • Sql Server2005对现有数据进行分区具体步骤

    2008-06-26 13:18:00
  • Python subprocess模块详细解读

    2023-11-17 02:50:01
  • 详解python多线程、锁、event事件机制的简单使用

    2022-03-16 19:48:25
  • python3+pyqt5+itchat微信定时发送消息的方法

    2022-02-28 04:31:49
  • ASP自动清除ACCESS数据库的日文字符

    2007-11-28 17:40:00
  • ASP伪静态页简单教程

    2007-09-28 14:35:00
  • asp之家 网络编程 m.aspxhome.com