检测递归调用(Detecting recursive calls)

编程入门 行业动态 更新时间:2024-10-24 01:56:48
检测递归调用(Detecting recursive calls)

检测递归方法调用的最佳方法是什么? 也许,它可以制成这种格式:

def foo if some_code_to_detect_recursive_call ... else ... end end

我可以想一种解析caller信息的方法,但我有兴趣看看是否有更好的方法。

What is the best way to detect recursive method calls? Perhaps, it can be made into this format:

def foo if some_code_to_detect_recursive_call ... else ... end end

I can think of a way parsing the caller information, but I am interested in seeing if there are better ways.

最满意答案

只需在输入方法后递增计数器(实例或全局变量),然后在退出时递减计数器。

在输入时,计数器会告诉您递归级别。

Just increment a counter (an instance or global variable) after entry to your method and decrement it on exit.

At entry, the counter tells you your recursion level.

更多推荐

本文发布于:2023-08-03 07:46:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1385268.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:递归   Detecting   calls   recursive

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!