【发布时间】:2012-12-08 17:16:20
【问题描述】:
假设我有两种方法first_method 和second_method 如下。
def first_method(some_arg):
"""
This is the method that calls the second_method
"""
return second_method(some_arg[1], some_arg[2])
def second_method(arg1, arg2):
"""
This method can only be called from the first_method
or it will raise an error.
"""
if (some condition):
#Execute the second_method
else:
raise SomeError("You are not authorized to call me!")
如何检查(什么条件)第一种方法正在调用第二种方法并根据该方法处理该方法?
【问题讨论】:
-
你有一些选择......要么使用闭包、装饰器,要么传递某种状态对象——如果对象处于无效状态,那么它可以引发
SomeError。 -
这个[stackoverflow.com/questions/1095543/… 帖子讨论了同样的问题。希望对您有所帮助。
标签: python django methods call