In Django's source code, there are **kwargs
and **initkwargs
. django/base.py at master · django/django
在Django的源代码中,有**kwargs和**initkwargs。django /基地。py at master·django/django
class View:
def __init__(self, **kwargs):
"""
Constructor. Called in the URLconf; can contain helpful extra
keyword arguments, and other things.
"""
# Go through keyword arguments, and either save their values to our
# instance, or raise an error.
for key, value in kwargs.items():
setattr(self, key, value)
and
和
@classonlymethod
def as_view(cls, **initkwargs):
"""Main entry point for a request-response process."""
for key in initkwargs:
if key in cls.http_method_names:
raise TypeError("You tried to pass in the %s method name as a "
"keyword argument to %s(). Don't do that."
% (key, cls.__name__))
What's difference of them in usage?
它们在使用上有什么不同?
2 个解决方案
#1
1
While kwargs
is the conventional name, the main reason it is called initkwargs
is to avoid name conflicts:
虽然kwargs是传统名称,但它被称为initkwargs的主要原因是为了避免名称冲突:
@classonlymethod
def as_view(cls, **initkwargs):
"""Main entry point for a request-response process."""
...
def view(request, *args, **kwargs): # defines kwargs
self = cls(**initkwargs) # uses initkwargs
...
return self.dispatch(request, *args, **kwargs)
...
return view
Note that the inner view
function takes a **kwargs
parameter. If the classmethod used the same name, the inner **kwargs
would shadow the outer **kwargs
, and the function wouldn't be able to access the outer kwargs
when instantiating cls
.
注意,内部视图函数使用**kwargs参数。如果classmethod使用相同的名称,那么内部的**kwargs将对外部的**kwargs进行阴影处理,在实例化cls时,函数将无法访问外部的kwargs。
Using the name initkwargs
avoids this issue.
使用initkwargs名称可以避免这个问题。
#2
0
As a comment says, there is no difference between the two regarding the mechnics behind it. The important part is the **
. That being said, I'd like to add the distinction from the two seems to comes from the fact that **initkwargs
is use for class instanciation. Indeed, it is always use in conjunction with cls
which normally stands for the first argument to class methods (PEP 8).
正如一篇评论所说,这两者对于背后的机制并没有什么区别。重要的部分是**。话虽如此,我还是想补充一下两者之间的区别似乎来自于**initkwargs用于类实例化这一事实。实际上,它总是与cls一起使用,cls通常代表类方法的第一个参数(PEP 8)。
So even though there is not much difference between the two and that it may only be a good pratice, I would not say that this is irrelevant.
因此,尽管这两者之间并没有太大的区别,这可能只是一个很好的实践,但我不会说这是无关紧要的。
#1
1
While kwargs
is the conventional name, the main reason it is called initkwargs
is to avoid name conflicts:
虽然kwargs是传统名称,但它被称为initkwargs的主要原因是为了避免名称冲突:
@classonlymethod
def as_view(cls, **initkwargs):
"""Main entry point for a request-response process."""
...
def view(request, *args, **kwargs): # defines kwargs
self = cls(**initkwargs) # uses initkwargs
...
return self.dispatch(request, *args, **kwargs)
...
return view
Note that the inner view
function takes a **kwargs
parameter. If the classmethod used the same name, the inner **kwargs
would shadow the outer **kwargs
, and the function wouldn't be able to access the outer kwargs
when instantiating cls
.
注意,内部视图函数使用**kwargs参数。如果classmethod使用相同的名称,那么内部的**kwargs将对外部的**kwargs进行阴影处理,在实例化cls时,函数将无法访问外部的kwargs。
Using the name initkwargs
avoids this issue.
使用initkwargs名称可以避免这个问题。
#2
0
As a comment says, there is no difference between the two regarding the mechnics behind it. The important part is the **
. That being said, I'd like to add the distinction from the two seems to comes from the fact that **initkwargs
is use for class instanciation. Indeed, it is always use in conjunction with cls
which normally stands for the first argument to class methods (PEP 8).
正如一篇评论所说,这两者对于背后的机制并没有什么区别。重要的部分是**。话虽如此,我还是想补充一下两者之间的区别似乎来自于**initkwargs用于类实例化这一事实。实际上,它总是与cls一起使用,cls通常代表类方法的第一个参数(PEP 8)。
So even though there is not much difference between the two and that it may only be a good pratice, I would not say that this is irrelevant.
因此,尽管这两者之间并没有太大的区别,这可能只是一个很好的实践,但我不会说这是无关紧要的。