Import object from builtins affecting just one class
我正在使用
我在forms.py中有一个类,它是:
1 2 3 4 5 | class Address: ...rest of code... class AddressForm(Address, forms.ModelForm): ...rest of code... |
在Python 2中
转换为:
1 2 3 4 5 6 | from buitlins import object class Address(object): ...rest of code... class AddressForm(Address, forms.ModelForm): ...rest of code... |
在Python 3中
我有一个Selenium测试,它在转换为python3后调用此表单时失败,错误如下:
1 2 3 4 5 | 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 |
但是,当我移除进口
但是,当我添加了一个未来检查时,我会得到一个未来差异错误,因此每个类都必须转换为NewStyle。我希望它能同时适用于python2和python3。
此模块
您遇到的问题似乎来自于两个不同的python 2现代化工具之间的冲突。你好像在使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | def python_2_unicode_compatible(klass): """ A decorator that defines __unicode__ and __str__ methods under Python 2. Under Python 3 it does nothing. To support Python 2 and 3 with a single code base, define a __str__ method returning text and apply this decorator to the class. """ if PY2: if '__str__' not in klass.__dict__: raise ValueError("@python_2_unicode_compatible cannot be applied" "to %s because it doesn't define __str__()." % klass.__name__) klass.__unicode__ = klass.__str__ klass.__str__ = lambda self: self.__unicode__().encode('utf-8') return klass |
继承了
1 2 3 4 5 6 7 8 9 10 11 | def __unicode__(self): # All subclasses of the builtin object should have __str__ defined. # Note that old-style classes do not have __str__ defined. if hasattr(self, '__str__'): s = type(self).__str__(self) else: s = str(self) if isinstance(s, unicode): return s else: return s.decode('utf-8') |
由于这两种方法在提供
提供builtins.object的模块提供自己的
这是Python之路。
1 | class Address(object): |
在python3类中,隐式继承对象,因此应该是这样的;
1 | class Address: |