在下面的方法定义,什么是*和**为做param2?
def foo(param1, *param2):
def bar(param1, **param2):
Run Code Online (Sandbox Code Playgroud) python syntax parameter-passing variadic-functions argument-unpacking
*运算符在Python 中的含义是什么,例如在代码中zip(*x)或f(**k)?
python syntax parameter-passing argument-unpacking iterable-unpacking
我无法理解这些函数的使用位置以及这些参数与普通参数的不同之处.我遇到过很多次,但从来没有机会正确理解它们.
例如:
def method(self, *links, **locks):
#some foo
#some bar
return
Run Code Online (Sandbox Code Playgroud)
我知道我可以搜索文档,但我不知道要搜索什么.
如果没有子类化dict,那么类需要被视为一个映射,以便它可以传递给**的方法
from abc import ABCMeta
class uobj:
__metaclass__ = ABCMeta
uobj.register(dict)
def f(**k): return k
o = uobj()
f(**o)
# outputs: f() argument after ** must be a mapping, not uobj
Run Code Online (Sandbox Code Playgroud)
至少到它抛出缺少映射功能的错误,所以我可以开始实现.
我查看了模拟容器类型,但只是定义魔术方法没有效果,并且使用ABCMeta覆盖并将其注册为dict将断言验证为子类,但是失败是isinstance(o,dict).理想情况下,我甚至不想使用ABCMeta.
在Java中,您可以使用构建器模式提供更具可读性的方法来实例化具有许多参数的类.在构建器模式中,构造一个配置对象,其中包含设置命名属性的方法,然后使用它来构造另一个对象.
Python中的等价物是什么?是模仿相同实现的最佳方法吗?
我的代码:
import threading
def hello(arg, kargs):
print arg
t = threading.Timer(2, hello, "bb")
t.start()
while 1:
pass
Run Code Online (Sandbox Code Playgroud)
打印输出只是:
b
Run Code Online (Sandbox Code Playgroud)
如何将参数传递给回调?kargs是什么意思?
正如PythonCookbook中提到的,*可以在元组之前添加,*这里的意思是什么?
第1.18章.将名称映射到序列元素:
from collections import namedtuple
Stock = namedtuple('Stock', ['name', 'shares', 'price'])
s = Stock(*rec)
# here rec is an ordinary tuple, for example: rec = ('ACME', 100, 123.45)
Run Code Online (Sandbox Code Playgroud)
在同一部分,**dict提出:
from collections import namedtuple
Stock = namedtuple('Stock', ['name', 'shares', 'price', 'date', 'time'])
# Create a prototype instance
stock_prototype = Stock('', 0, 0.0, None, None)
# Function to convert a dictionary to a Stock
def dict_to_stock(s):
return stock_prototype._replace(**s)
Run Code Online (Sandbox Code Playgroud)
什么是**这里的功能?
我试图找到一组集合的联合.具体来说,我想要networkx调用图表字典中每个键的节点列表的并集periodic_gs.我想使用该reduce函数,因为似乎合理的是将所有periodic_gs[x].nodes() where 的联合作为x一个关键periodic_gs.
这是我的尝试:
reduce(lambda x,y: set(periodic_gs[x].nodes()).union(set(periodic_gs[y].nodes())), periodic_gs.keys(), {})
Run Code Online (Sandbox Code Playgroud)
对我而言,这表示在字典中的每个图形上使用节点的并集.出于某种原因,python告诉我:TypeError: unhashable type: 'dict'我没有看到这个TypeError,因为periodic_gs.keys()是一个键列表(它们是字符串,但我不知道这是多么重要),并且当替换为lambda函数的参数时将起作用.
是什么导致类型错误,我该如何解决?
简单程序:
storyFormat = """
Once upon a time, deep in an ancient jungle,
there lived a {animal}. This {animal}
liked to eat {food}, but the jungle had
very little {food} to offer. One day, an
explorer found the {animal} and discovered
it liked {food}. The explorer took the
{animal} back to {city}, where it could
eat as much {food} as it wanted. However,
the {animal} became homesick, so the
explorer brought it back to the jungle,
leaving …Run Code Online (Sandbox Code Playgroud) 我有以下功能:
def create_job(target, args):
p = multiprocessing.Process(target=target, args=args)
p.start()
return p
Run Code Online (Sandbox Code Playgroud)
上面的函数被调用:
ps = create_job(mc.collect_categories, (cat_ids),)
Run Code Online (Sandbox Code Playgroud)
作为 cat_ids 'ids' 列表:['ML68', 'MA453','MA33']
当我运行代码时,出现以下错误(包含 10 个 cat_ids 的列表):
TypeError: collect_categories() takes exactly 2 arguments (11 given)
Run Code Online (Sandbox Code Playgroud)
我尝试用“*”传递值来解压它,但这不是问题。
python ×10
python-3.x ×2
syntax ×2
class ×1
dictionary ×1
fold ×1
mapping ×1
namedtuple ×1
set ×1
timer ×1
tuples ×1