In Django documentation django.conf.urls | Django documentation | Django
There's such codes:
class RegexURLPattern(LocaleRegexProvider):
...
def lookup_str(self)
callback = self.callback
# Python 3.5 collapses nested partials, so can change "while" to "if"
# when it's the minimum supported version.
while isinstance(callback, functools.partial):
callback = callback.func
...
The code is to callback the argument of funtion 'view' and execute it.
Why while
is better than if
excluding 'Python 3.5'?
There is only one callback argument passed in from django.conf.urls.url
,which seen in:
def url(regex, view, kwargs=None, name=None):
elif callable(view):
return RegexURLPattern(regex, view, kwargs, name)
It seems if
is more readable than while
.
It's not about number of arguments but about the depth of how many times a callback was wrapped in another callback.
For one iteration, while
is the same as if
so that doesn't really matter. However, if someone called lookup_str
with callback which was itself a callback, while
would still just keep on to work.