在对另一个问题的答案的评论中,有人说他们不确定functools.wraps在做什么.所以,我问这个问题,以便在StackOverflow上有一个记录,供将来参考:究竟functools.wraps做了什么?
我无法理解functools中的部分工作原理.我从这里得到以下代码:
>>> sum = lambda x, y : x + y
>>> sum(1, 2)
3
>>> incr = lambda y : sum(1, y)
>>> incr(2)
3
>>> def sum2(x, y):
return x + y
>>> incr2 = functools.partial(sum2, 1)
>>> incr2(4)
5
Run Code Online (Sandbox Code Playgroud)
现在就行了
incr = lambda y : sum(1, y)
Run Code Online (Sandbox Code Playgroud)
我得到的是,无论我传递给incr它的任何论点都将被传递y给lambda哪个将返回sum(1, y)ie 1 + y.
我明白那个.但我不明白这一点incr2(4).
如何在部分函数中4传递x?对我来说,4应该更换sum2.x和之间有什么关系 …
我正在尝试使用另一个更通用的类方法定义一些类方法,如下所示:
class RGB(object):
def __init__(self, red, blue, green):
super(RGB, self).__init__()
self._red = red
self._blue = blue
self._green = green
def _color(self, type):
return getattr(self, type)
red = functools.partial(_color, type='_red')
blue = functools.partial(_color, type='_blue')
green = functools.partial(_color, type='_green')
Run Code Online (Sandbox Code Playgroud)
但当我尝试调用任何这些方法时,我得到:
rgb = RGB(100, 192, 240)
print rgb.red()
TypeError: _color() takes exactly 2 arguments (1 given)
Run Code Online (Sandbox Code Playgroud)
我想_color自从rgb.red(rgb)工作以来,自我没有被传递.
如何在不泄漏内存的情况下在类中使用functools的lru_cache?在下面的最小示例中,foo虽然超出范围且没有引用者(lru_cache除外),但实例不会被释放.
from functools import lru_cache
class BigClass:
pass
class Foo:
def __init__(self):
self.big = BigClass()
@lru_cache(maxsize=16)
def cached_method(self, x):
return x + 5
def fun():
foo = Foo()
print(foo.cached_method(10))
print(foo.cached_method(10)) # use cache
return 'something'
fun()
Run Code Online (Sandbox Code Playgroud)
但是foo因此foo.big(a BigClass)仍然活着
import gc; gc.collect() # collect garbage
len([obj for obj in gc.get_objects() if isinstance(obj, Foo)]) # is 1
Run Code Online (Sandbox Code Playgroud)
这意味着Foo/BigClass实例仍然驻留在内存中.即使删除Foo(del Foo)也不会释放它们.
为什么lru_cache会依赖实例?缓存不是使用一些哈希而不是实际对象吗?
在类中使用lru_caches的推荐方法是什么?
我怎样才能使@functools.lru_cache装饰器忽略一些关于缓存键的函数参数?
例如,我有一个如下所示的函数:
def find_object(db_handle, query):
# (omitted code)
return result
Run Code Online (Sandbox Code Playgroud)
如果我lru_cache像这样应用装饰器,db_handle将包含在缓存键中.因此,如果我尝试使用相同query但不同的函数调用该函数db_handle,它将再次执行,我想避免.我只想lru_cache考虑query参数.
我一直在使用Python,我设置了以下代码情况:
import timeit
setting = """
import functools
def f(a,b,c):
pass
g = functools.partial(f,c=3)
h = functools.partial(f,b=5,c=3)
i = functools.partial(f,a=4,b=5,c=3)
"""
print timeit.timeit('f(4,5,3)', setup = setting, number=100000)
print timeit.timeit('g(4,5)', setup = setting, number=100000)
print timeit.timeit('h(4)', setup = setting, number=100000)
print timeit.timeit('i()', setup = setting, number=100000)
Run Code Online (Sandbox Code Playgroud)
我得到以下结果:
f: 0.181384086609
g: 0.39066195488
h: 0.425783157349
i: 0.391901016235
Run Code Online (Sandbox Code Playgroud)
为什么对部分函数的调用需要更长时间?部分函数是将参数转发到原始函数还是映射静态参数?而且,在Python中是否有一个函数返回一个函数的主体,假定所有参数都是预定义的,就像函数i一样?
我目前正在使用EndpointsModel为AppEngine上的所有模型创建RESTful API.由于它是RESTful,这些api有很多重复代码,我想避免
例如:
class Reducer(EndpointsModel):
name = ndb.StringProperty(indexed=False)
@endpoints.api(
name="bigdata",
version="v1",
description="""The BigData API""",
allowed_client_ids=ALLOWED_CLIENT_IDS,
)
class BigDataApi(remote.Service):
@Reducer.method(
path="reducer",
http_method="POST",
name="reducer.insert",
user_required=True,
)
def ReducerInsert(self, obj):
pass
## and GET, POST, PUT, DELETE
## REPEATED for each model
Run Code Online (Sandbox Code Playgroud)
我想让它们变得通用.所以我尝试动态添加方法到类.到目前为止我尝试了什么:
from functools import partial, wraps
def GenericInsert(self, obj, cls):
obj.owner = endpoints.get_current_user()
obj.put()
return obj
# Ignore GenericDelete, GenericGet, GenericUpdate ...
import types
from functools import partial
def register_rest_api(api_server, endpoint_cls):
name = endpoint_cls.__name__
# create list method
query_method = types.MethodType( …Run Code Online (Sandbox Code Playgroud) python google-app-engine functools google-cloud-endpoints endpoints-proto-datastore
在Python 3.3中,itertools.accumulate()通常会对提供的iterable重复应用加法运算,现在可以将函数参数作为参数; 这意味着它现在重叠functools.reduce().粗略看一下,现在两者之间的主要区别似乎是:
accumulate()默认为求和但不允许显式提供额外的初始条件,reduce()而不默认为任何方法但允许您提供初始条件以使用1/0元素序列,并且accumulate()首先采用迭代,然后先reduce()获取函数.这两者之间还有其他差异吗?或者这仅仅是两个函数的行为问题,最初的不同用途开始随时间收敛?
正确的类型提示是什么functools.partial?我有一个返回 a 的函数partial,我想输入提示,这样就mypy不会抛出任何错误:
def my_func() -> ?:
return partial(foo, bar="baz")
Run Code Online (Sandbox Code Playgroud)
比更具体typing.Callable
CPython 3.6.4:
from functools import partial
def add(x, y, z, a):
return x + y + z + a
list_of_as = list(range(10000))
def max1():
return max(list_of_as , key=lambda a: add(10, 20, 30, a))
def max2():
return max(list_of_as , key=partial(add, 10, 20, 30))
Run Code Online (Sandbox Code Playgroud)
现在:
In [2]: %timeit max1()
4.36 ms ± 42.3 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [3]: %timeit max2()
3.67 ms ± 25.9 µs per loop (mean ± std. dev. of …Run Code Online (Sandbox Code Playgroud)