在下面的方法定义,什么是*和**为做param2?
def foo(param1, *param2):
def bar(param1, **param2):
Run Code Online (Sandbox Code Playgroud) python syntax parameter-passing variadic-functions argument-unpacking
所以我有这个概念的困难*args和**kwargs.
到目前为止,我已经了解到:
*args =参数列表 - 作为位置参数**kwargs = dictionary - 其键成为单独的关键字参数,值成为这些参数的值.我不明白这会对哪些编程任务有所帮助.
也许:
我想输入列表和字典作为函数AND的参数同时作为通配符,所以我可以传递任何参数?
有一个简单的例子来说明如何*args和**kwargs使用?
我发现的教程也使用了"*"和变量名.
是*args和**kwargs刚才占位符或者你使用完全相同*args,并**kwargs在代码中?
我已经阅读了python文档中的示例,但仍然无法弄清楚这个方法的含义.有人可以帮忙吗?以下是python文档中的两个示例
>>> from collections import defaultdict
>>> s = 'mississippi'
>>> d = defaultdict(int)
>>> for k in s:
... d[k] += 1
...
>>> d.items()
[('i', 4), ('p', 2), ('s', 4), ('m', 1)]
Run Code Online (Sandbox Code Playgroud)
和
>>> s = [('yellow', 1), ('blue', 2), ('yellow', 3), ('blue', 4), ('red', 1)]
>>> d = defaultdict(list)
>>> for k, v in s:
... d[k].append(v)
...
>>> d.items()
[('blue', [2, 4]), ('red', [1]), ('yellow', [1, 3])]
Run Code Online (Sandbox Code Playgroud)
参数int和list用于什么?
可能重复:
了解Python中的kwargs
我已经阅读了一段python代码,我不知道在这段代码中*和**的含义是什么:
def functionA(self, *a, **kw):
// code here
Run Code Online (Sandbox Code Playgroud)
我只知道*的一个用法:提取它对方法或构造函数的参数的所有属性.
如果这对于上述函数是真的,那么其余的是什么:**?
我正在尝试使用过滤查询集
info=members.filter(name__contains=search_string)
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是我不知道用户想要提前搜索哪个字段所以我需要用变量替换'name',如
variable_column = 'name'
search_type = 'contains'
filter = variable_column + '__' + search_type
info=members.filter(filter=search_string)
Run Code Online (Sandbox Code Playgroud)
我怎么做?
丰富
假设我有一些功能, f
def f (a=None):
print a
Run Code Online (Sandbox Code Playgroud)
现在,如果我有一本字典dct = {"a":"Foo"},我可以调用f(**dct)并Foo打印结果.
但是,假设我有一本字典dct2 = {"a":"Foo", "b":"Bar"}.如果我打电话给f(**dct2)我一个
TypeError: f() got an unexpected keyword argument 'b'
Run Code Online (Sandbox Code Playgroud)
很公平.但是,无论如何,在f调用它的定义或调用它时,告诉python只是忽略任何不是参数名称的键?优选的是允许指定默认值的方法.
我有一个函数定义如下,我传递的是关键字参数.如何返回与关键字参数同名的字典?
我可以手动做:
def generate_student_dict(first_name=None, last_name=None , birthday=None, gender =None):
return {
'first_name': first_name,
'last_name': last_name,
'birthday': birthday,
'gender': gender
}
Run Code Online (Sandbox Code Playgroud)
但我不想这样做.有没有什么方法可以让我在没有实际输入字典的情况下完成这项工作?
def generate_student_dict(self, first_name=None, last_name=None, birthday=None, gender=None):
return # Packed value from keyword argument.
Run Code Online (Sandbox Code Playgroud) 我知道星号在Python中的函数定义中是什么意思.
不过,我经常会看到使用参数调用函数的星号:
def foo(*args, **kwargs):
first_func(args, kwargs)
second_func(*args, **kwargs)
Run Code Online (Sandbox Code Playgroud)
第一个和第二个函数调用有什么区别?
在这些例子中,我经常看到**kwargs四处传播,没有提到它来自哪里:
from django.views.generic import DetailView
from books.models import Publisher, Book
class PublisherDetailView(DetailView):
context_object_name = "publisher"
model = Publisher
def get_context_data(self, **kwargs):
# Call the base implementation first to get a context
context = super(PublisherDetailView, self).get_context_data(**kwargs)
# Add in a QuerySet of all the books
context['book_list'] = Book.objects.all()
return context
Run Code Online (Sandbox Code Playgroud)
**kwargs从哪里神奇地被拔掉?
此外,这似乎只是添加单个字典对象的额外工作吗?
我是Python的新手.我需要创建一个简单的学生课程,其中包括名字,姓氏,身份证和将课程名称映射到其成绩的字典.
class Student:
def __init__(self, firstName, lastName, id, _____ (dictionary values)):
self._firstName = firstName;
self._lastName = lastName;
self._id = id;
self.
Run Code Online (Sandbox Code Playgroud)
我的问题是如何在构造函数中初始化字典值?
例如,假设我想在等级映射中添加3门课程:"数学:100""生物:90""历史:80"
例如:
student1 = Student("Edward", "Gates", "0456789", math: 100, bio: 90, history: 80)
Run Code Online (Sandbox Code Playgroud)
最后3个值应该进入字典.
由于可以作为字典一部分的键值的数量可以变化,我应该在构造函数参数签名中写什么?
我想在调用构造函数时发送所有学生值...
python ×8
dictionary ×3
django ×2
args ×1
defaultdict ×1
function ×1
kwargs ×1
python-2.7 ×1
syntax ×1