[英]_args and _kwargs arguments for python
#!/usr/bin/env python
class Countries(object):
def __init__(self,*_args,**_kwargs):
self.name = 'japan'
self.continent = 'asia'
self.capital = 'tokyo'
print _args
print _kwargs
def display(self):
print self.name, self.continent,self.capital
iraq = Countries('iraq','bagdad','asia')
iraq.display()
india = Countries('india','delhi','asia')
india.display()
I have written a self teaching example above.I know that irrespective of the parameters I am always assigning 我上面写了一个自学教学例子。我知道无论我总是分配什么参数
'japan' , 'asia' , 'tokyo'
to the member variables of the class.I wrote the above in an attempt to understand 到类的成员变量。我写了上面的内容,试图理解
*_args and **_kwargs
The output of the above program is as below 上述程序的输出如下
('iraq', 'bagdad', 'asia')
{}
japan asia tokyo
('india', 'delhi', 'asia')
{}
japan asia tokyo
where _args is ('iraq', 'bagdad', 'asia')
其中_args是
('iraq', 'bagdad', 'asia')
and __kwargs is {}
和__kwargs是
{}
why is a null string being printed for _kwargs
and all arguments printed as a list for _args
为什么被印刷为空字符串
_kwargs
和所有参数打印为列表_args
Because you aren't passing in any keyword arguments. 因为您没有传递任何关键字参数。 If you did
Countries('Iraq', capital='Bagdad')
, then it would print something for kwargs. 如果你做了
Countries('Iraq', capital='Bagdad')
,那么它会为kwargs打印一些东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.