我有一个函数,我想查看并查看正在使用的请求类型,然后根据选择的函数运行另一个函数。
from tests.global_functions.util_helper import util_get_random_customer_individual
from tests.global_functions.util_helper import util_get_random_customer_company
import requests
def __init__(self):
request = requests.Requests()
if request == requests.Requests().data_add_customer_individual():
customer = util_get_random_customer_individual()
else:
customer = util_get_random_customer_company()
当我尝试运行此功能时,我收到了RuntimeError: maximum recursion depth exceeded while calling a Python object
如果我将import requests
更改为from requests import Requests
,则会收到错误消息,指出无法导入请求
我做错了什么?
我尝试了以下操作:
request = requests.Requests
if request == requests.Requests.data_add_customer_individual():
customer = util_get_random_customer_individual()
else:
customer = util_get_random_customer_company()
我仍然得到递归深度超出错误。
这是追溯:
File "C:\Users\e003048\QA\trunk\automation\selenium\src\webservices\system_environment\customer.py", line 26, in __init__
if request == requests.Requests.data_add_customer_individual():
File "C:\Users\e003048\QA\trunk\automation\selenium\src\webservices\system_environment\requests.py", line 85, in data_add_customer_individual
my_request = request_data.RequestData()
RuntimeError: maximum recursion depth exceeded while calling a Python object
答案 0 :(得分:2)
这个类有没有被称为Requests
,它是否存在于名为requests
的模块中?它看起来很像你在类自己的构造函数中创建一个新对象。回溯中显示的行肯定会告诉您。
答案 1 :(得分:1)
看起来你对data_add_customer_individual
的调用会导致创建当前类的新对象(因为你已经删除了class
语句我不知道叫什么)。这导致递归对象创建,因为新对象也将调用data_add_customer_individual
,这将创建另一个新对象等。
这是一个做同样事情的小例子:
class Foo(object):
def __init__(self):
Bar()
def bar():
foo = Foo() # create a new Foo instance
print foo # this won't ever get reached, since the recursion happens above
通常,修复此类递归的方法是将Foo
对象的实例传递给bar
:
class Foo(object):
def __init__(self):
bar(self) # pass ourself as a reference!
def bar(foo): # bar now takes foo as a parameter, rather than creating it
print foo # this will work
答案 2 :(得分:0)
因为我一遍又一遍地调用自己的变量,导致了最大递归错误。为了解决这个问题,我在名为Requests()
的{{1}}类中添加了一个变量,然后在我的request_type = None
函数中,我声明add_customer_individual
add_customer_company`函数只命名请求类型公司。
然后在我的客户课程中,我做了以下事情:
request_type = 'individual' and the same for the
一旦我发现为什么我不能使用`from requests import Requests
,这应该可以工作