apply() 函数和使用 class 对象的函数调用有什么区别?
What is the difference between the apply() function and a function call using the object of the class?
将 Atom 视为 class 其中
- form.name 是一个字符串
- 转换returns值列表
下面两行有什么区别?
apply(Atom, [form.name] + list([convert(arg, subst) for arg in
list(form.args)]))
Atom(form.name, [convert(arg, subst) for arg in form.args])
来自文档,
apply(...)
apply(object[, args[, kwargs]]) -> value
Call a callable object with positional arguments taken from the tuple args,
and keyword arguments taken from the optional dictionary kwargs.
Note that classes are callable, as are instances with a call() method.
我无法理解这两条线之间的区别。我试图在 Python 3.5
中找到 apply(Atom, [form.name] + list([convert(arg, subst) for arg in list(form.args)]))
的等效代码
apply
是 unpacking arguments 的老式 1 方式。换句话说,以下都产生相同的结果:
results = apply(foo, [1, 2, 3])
results = foo(*[1, 2, 3])
results = foo(1, 2, 3)
由于您在 python3.5 中工作,其中 apply
已不存在,因此该选项无效。此外,您将参数作为列表使用,因此您也不能真正使用第三个选项。剩下的唯一选择是第二个。我们可以很容易地将您的表达式转换为该格式。 python3.5 中的等价物是:
Atom(*([form.name] + [convert(arg, subst) for arg in list(form.args)]))
1已在 python2.3!
中弃用
将 Atom 视为 class 其中
- form.name 是一个字符串
- 转换returns值列表
下面两行有什么区别?
apply(Atom, [form.name] + list([convert(arg, subst) for arg in list(form.args)]))
Atom(form.name, [convert(arg, subst) for arg in form.args])
来自文档,
apply(...) apply(object[, args[, kwargs]]) -> value
Call a callable object with positional arguments taken from the tuple args, and keyword arguments taken from the optional dictionary kwargs. Note that classes are callable, as are instances with a call() method.
我无法理解这两条线之间的区别。我试图在 Python 3.5
中找到apply(Atom, [form.name] + list([convert(arg, subst) for arg in list(form.args)]))
的等效代码
apply
是 unpacking arguments 的老式 1 方式。换句话说,以下都产生相同的结果:
results = apply(foo, [1, 2, 3])
results = foo(*[1, 2, 3])
results = foo(1, 2, 3)
由于您在 python3.5 中工作,其中 apply
已不存在,因此该选项无效。此外,您将参数作为列表使用,因此您也不能真正使用第三个选项。剩下的唯一选择是第二个。我们可以很容易地将您的表达式转换为该格式。 python3.5 中的等价物是:
Atom(*([form.name] + [convert(arg, subst) for arg in list(form.args)]))
1已在 python2.3!
中弃用