我在 python 中有一些代码,它按位或等于 b 到一个称为 a 的多维列表中的所有值
for i in xrange(len(a)):
for j in xrange(len(a[i])):
a[i][j] |= b
Run Code Online (Sandbox Code Playgroud)
我的问题是,有没有什么方法可以只使用 (map()、filter()、reduce()) 编写此代码,而不必使用 lambdas 或任何其他函数定义,如下例所示
map(lambda x: map(lambda y: y | b, x), a)
Run Code Online (Sandbox Code Playgroud) 说我有一个功能
def my_meta_function (a, b, c):
pass
Run Code Online (Sandbox Code Playgroud)
我想定义一个函数数组myfunctions = [f1, f2, f3, ... f100],其中参数c固定为每个这样的函数的不同值,例如c = [1,2,3, .. 100],函数只接受参数a和b.在实践中,我正在考虑的论点更复杂,但我试图理解如何在语言中这样做.
请原谅我标题中的不良措辞,但这里有一个更长的解释:
我有一个函数,作为参数需要一些函数,用于确定从数据库中检索哪些数据,如下所示:
def customer_data(customer_name, *args):
# initialize dictionary with ids
codata = dict([(data.__name__, []) for data in args])
codata['customer_observer_id'] = _customer_observer_ids(customer_name)
# add values to dictionary using function name as key
for data in args:
for coid in codata['customer_observer_id']:
codata[data.__name__].append(data(coid))
return codata
Run Code Online (Sandbox Code Playgroud)
这使得对函数的调用看起来像这样:
customer_data('customername', target_parts, source_group, ...)
Run Code Online (Sandbox Code Playgroud)
其中一个函数使用额外参数定义:
def polarization_value(customer_observer_id, timespan='day')
Run Code Online (Sandbox Code Playgroud)
我想要的是一种以巧妙的方式更改时间跨度变量的方法.一个显而易见的方法是在customer_observer中包含一个关键字参数,并在调用的函数名称为"polarization_value"时添加一个异常,但我觉得有更好的方法可以做到这一点.
注意: 我在问是否有一种 Pythonic 的方式来做到这一点(使用默认参数似乎比使用部分 Pythonic 更少),并且是否有任何一种方法的重大限制(“成本” - 我不希望时间有显着差异,但是也许还有其他限制我没有看到使平衡倾向于一种方法与另一种方法)。
我试图了解在 lambda 不可行的后期绑定情况下使用“部分”的成本。我已经根据本指南创建了一些示例代码来说明这一点。
由于后期绑定,以下内容无法按预期工作:
def create_thingies():
thingies = []
for i in range(1,6):
def thingy(x):
print("Some output", i)
return i ** (x * i)
thingies.append(thingy)
return thingies
results=[]
for thingy in create_thingies():
results.append(thingy(2))
print(results)
Run Code Online (Sandbox Code Playgroud)
输出:
Some output 5
Some output 5
Some output 5
Some output 5
Some output 5
[9765625, 9765625, 9765625, 9765625, 9765625]
Run Code Online (Sandbox Code Playgroud)
使用“部分”我们可以避免这个问题,但代价是什么?
from functools import partial
def create_thingies():
thingies = []
for i in range(1,6):
def …Run Code Online (Sandbox Code Playgroud) 我有一个类似的课程
class C:
def __init__(self, a):
self.a = a
def noParam(self):
return self.a
def withParam(self, b)
return self.a + b
instC = C(5.)
Run Code Online (Sandbox Code Playgroud)
我需要传递一个类的特定实例的方法作为参数.传instC.noParam工作正常,但我怎么通过instC.withParam与b总是等于说239?谢谢.
假设我们有一个基本功能:
def basic(arg):
print arg
Run Code Online (Sandbox Code Playgroud)
我们需要推迟在另一个函数中对该函数的求值。我正在考虑两种可能的方法:
使用 lambda:
def another(arg):
return lambda: basic(arg)
Run Code Online (Sandbox Code Playgroud)使用 functools.partial
from functools import partial
def another(arg):
return partial(basic, arg)
Run Code Online (Sandbox Code Playgroud)首选哪种方法?为什么?还有另一种方法可以做到这一点吗?
我非常喜欢functools.partialpython中的函数以及这个函数的概念.作为示例考虑以下python脚本(我知道这个案例不是一个非常有用的示例,使用functools.partial,它应该只是一个简单的例子.)
import functools
def func(a, b, c):
sum = a + b + c
return sum
if __name__ == "__main__":
func_p = functools.partial(func, a=1, c=1)
sum = func_p(b=1)
print(sum)
Run Code Online (Sandbox Code Playgroud)
C++中有没有提供类似功能的东西?
我正在学习偏见以及何时使用它们.在这个关于partials vs lambdas的页面中,接受的答案解释了partialsover 的优点之一lambdas是partials具有对内省有用的属性.因此我们可以使用partials来执行以下操作:
import functools
f = functools.partial(int, base=2)
print f.args, f.func, f.keywords
((), int, {'base': 2})
Run Code Online (Sandbox Code Playgroud)
实际上,我们不能这样做lambdas:
h = lambda x : int(x,base=2)
print h.args, h.func, h.keywords
AttributeError: 'function' object has no attribute 'args'
Run Code Online (Sandbox Code Playgroud)
但实际上,我们不能用"普通"Python函数做到这一点:
def g(x) :
return int(x,base=2)
print g.args, g.func, g.keywords
AttributeError: 'function' object has no attribute 'args'
Run Code Online (Sandbox Code Playgroud)
为什么partials比普通的Python函数有更多的功能?这种设计的目的是什么?内省被认为对正常功能无用吗?