mirror of
https://github.com/django/django.git
synced 2025-07-24 05:36:15 +00:00
Fixed #18963 -- Used a subclass-friendly pattern
for Python 2 object model compatibility methods.
This commit is contained in:
parent
973f539ab8
commit
fc10418fba
14 changed files with 61 additions and 50 deletions
|
@ -278,15 +278,13 @@ Iterators
|
|||
|
||||
::
|
||||
|
||||
class MyIterator(object):
|
||||
class MyIterator(six.Iterator):
|
||||
def __iter__(self):
|
||||
return self # implement some logic here
|
||||
|
||||
def __next__(self):
|
||||
raise StopIteration # implement some logic here
|
||||
|
||||
next = __next__ # Python 2 compatibility
|
||||
|
||||
Boolean evaluation
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
@ -297,7 +295,8 @@ Boolean evaluation
|
|||
def __bool__(self):
|
||||
return True # implement some logic here
|
||||
|
||||
__nonzero__ = __bool__ # Python 2 compatibility
|
||||
def __nonzero__(self): # Python 2 compatibility
|
||||
return type(self).__bool__(self)
|
||||
|
||||
Division
|
||||
~~~~~~~~
|
||||
|
@ -309,12 +308,14 @@ Division
|
|||
def __truediv__(self, other):
|
||||
return self / other # implement some logic here
|
||||
|
||||
__div__ = __truediv__ # Python 2 compatibility
|
||||
def __div__(self, other): # Python 2 compatibility
|
||||
return type(self).__truediv__(self, other)
|
||||
|
||||
def __itruediv__(self, other):
|
||||
return self // other # implement some logic here
|
||||
|
||||
__idiv__ = __itruediv__ # Python 2 compatibility
|
||||
def __idiv__(self, other): # Python 2 compatibility
|
||||
return type(self).__itruediv__(self, other)
|
||||
|
||||
.. module: django.utils.six
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue