我正在将代码从 python2 转换为 python3 用于newstyle
使用future
. 我的项目在 Django 1.11 中
我在 forms.py 中有一个类:
class Address:
...rest of code...
class AddressForm(Address, forms.ModelForm):
...rest of code...
在 Python 2 中
转换为:
from buitlins import object
class Address(object):
...rest of code...
class AddressForm(Address, forms.ModelForm):
...rest of code...
在 Python 3 中
我有一个 selenium 测试,它在转换为 Python3 后调用此表单时失败,并出现以下错误:
File "<path_to_venv>/local/lib/python2.7/site-packages/django/utils/six.py", line 842, in <lambda>
klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
File "<path_to_venv>/local/lib/python2.7/site-packages/future/types/newobject.py", line 78, in __unicode__
s = type(self).__str__(self)
RuntimeError: maximum recursion depth exceeded
但是,当我删除导入时from buitlins import object
,测试通过了。
但是当我添加了一个未来的检查时,我得到了一个未来的差异错误,因此每个类都必须转换为 newstyle。我希望它在 Python2 和 Python3 中都能工作。
有没有办法这个模块builtins
模块导入可以只影响一个类而不影响forms.py
文件中的其他类。还是有其他方法可以解决这个问题?