max*_*max 5 python lambda anonymous-function python-3.x
Python不支持复杂的匿名函数.什么是好的选择?例如:
class Calculation:
def __init__(self, func):
self.func = func
def __call__(self, data):
try:
# check if the value has already been calculated
# if it has, it would be cached under key = self.func
return data[self.func]
except KeyError:
pass # first-time call; calculate and cache the values
data[self.func] = self.func(data)
return data[self.func]
# with a simple function, which can be represented using lambda, this works great
f1 = Calculation(lambda data : data['a'] * data['b'])
# with a complicated function, I can do this:
def f2_aux:
# some complicated calculation, which isn't suitable for a lambda one-liner
f2 = Calculation(f2_aux)
Run Code Online (Sandbox Code Playgroud)
这是一个合理的设计吗?
如果是这样,有没有办法避免我在模块中定义的每个f*的f*_aux的丑陋?
更新:
使用示例:
d = {'a' : 3, 'b' : 6}
# computes 3 * 6
# stores 18 in d under a key <function <lambda> at ...>
# returns 18
f1(d)
# retrieves 18 from d[<function <lambda> at ...>]
# returns 18, without having to recalculate it
f1(d)
Run Code Online (Sandbox Code Playgroud)
更新:
仅仅为了我的理解,我添加了一个使用内部函数的版本.
def memoize(func):
def new_func(data):
try:
# check if the value has already been calculated
# if it has, it would be cached under key = self.func
return data[func]
except KeyError:
pass # first-time call; calculate and cache the values
data[func] = func(data)
return data[func]
return new_func
@memoize
def f1(data):
return data['a'] * data['b']
Run Code Online (Sandbox Code Playgroud)
小智 5
您不需要匿名功能.此外,memoization已经做得比这更好,你可能没有理由自己推出.
但要回答这个问题:您可以将您的班级用作装饰者.
@Calculation
def f2():
...
Run Code Online (Sandbox Code Playgroud)
这简单地定义了函数,将其包装Calculation
并存储为其结果f2
.装饰器语法定义为等效于:
_decorator = Calculation # a fresh identifier
# not needed here, but in other cases (think properties) it's useful
def f2():
...
f2 = _decorator(f2)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5639 次 |
最近记录: |