计算另一个方法中的python方法调用 [英] Counting python method calls within another method

查看:53
本文介绍了计算另一个方法中的python方法调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上是在尝试用 Java 来做这件事,但我正在自学 python,这让我想知道是否有一种简单/聪明的方法可以用包装器或其他东西来做到这一点.

我想知道一个特定方法在另一个方法中被调用了多少次.例如:

def foo(z):#做一点事返回结果定义栏(x,y):#涉及 foo 的复杂算法/逻辑返回 foobar

因此,对于每次使用各种参数调用 bar 时,我想知道 foo 被调用了多少次,可能输出如下:

<预><代码>>>>打印栏('xyz',3)foo 被调用了 15 次[结果在这里]>>>打印栏('stuv',6)foo 被调用了 23 次[这里的其他结果]

我意识到我可以在酒吧里打一个柜台,然后在我回来时把它扔掉,但是如果你能用包装纸做一些魔法来完成同样的事情,那就太酷了.这也意味着我可以在其他地方重用相同的包装器,而无需修改方法内的任何代码.

解决方案

听起来几乎是装饰者的教科书示例!

def 计数(fn):def 包装器(*args, **kwargs):包装器调用 += 1返回 fn(*args, **kwargs)包装器调用 = 0wrapper.__name__ = fn.__name__返回包装器@计数定义 foo():返回>>>富()>>>foo.调用1

你甚至可以使用另一个装饰器来自动记录一个函数在另一个函数中被调用的次数:

def 计数(其他):定义装饰器(fn):def 包装器(*args, **kwargs):其他.调用 = 0尝试:返回 fn(*args, **kwargs)最后:打印 '%s 被调用了 %i 次' % (other.__name__, other. called)wrapper.__name__ = fn.__name__返回包装器返回装饰器@计数(富)定义栏():富()富()>>>酒吧()foo 被调用了 2 次

如果 foobar 最终可以调用自己,那么您需要一个更复杂的解决方案,包括堆栈来处理递归.然后你将走向一个完整的分析器......

如果您仍在自学 Python",那么这种包装装饰器的东西(通常用于魔术)可能不是您寻找的理想场所!

I'm actually trying doing this in Java, but I'm in the process of teaching myself python and it made me wonder if there was an easy/clever way to do this with wrappers or something.

I want to know how many times a specific method was called inside another method. For example:

def foo(z):
    #do something
    return result

def bar(x,y):
    #complicated algorithm/logic involving foo
    return foobar

So for each call to bar with various parameters, I'd like to know how many times foo was called, perhaps with output like this:

>>> print bar('xyz',3)
foo was called 15 times
[results here]
>>> print bar('stuv',6)
foo was called 23 times
[other results here]

edit: I realize I could just slap a counter inside bar and dump it when I return, but it would be cool if there was some magic you could do with wrappers to accomplish the same thing. It would also mean I could reuse the same wrappers somewhere else without having to modify any code inside the method.

解决方案

Sounds like almost the textbook example for decorators!

def counted(fn):
    def wrapper(*args, **kwargs):
        wrapper.called += 1
        return fn(*args, **kwargs)
    wrapper.called = 0
    wrapper.__name__ = fn.__name__
    return wrapper

@counted
def foo():
    return

>>> foo()
>>> foo.called
1

You could even use another decorator to automate the recording of how many times a function is called inside another function:

def counting(other):
    def decorator(fn):
        def wrapper(*args, **kwargs):
            other.called = 0
            try:
                return fn(*args, **kwargs)
            finally:
                print '%s was called %i times' % (other.__name__, other.called)
        wrapper.__name__ = fn.__name__
        return wrapper
    return decorator

@counting(foo)
def bar():
    foo()
    foo()

>>> bar()
foo was called 2 times

If foo or bar can end up calling themselves, though, you'd need a more complicated solution involving stacks to cope with the recursion. Then you're heading towards a full-on profiler...

Possibly this wrapped decorator stuff, which tends to be used for magic, isn't the ideal place to be looking if you're still ‘teaching yourself Python’!

这篇关于计算另一个方法中的python方法调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆