从lambda获得价值

时间:2016-05-22 11:59:33

标签: python lambda

我怎么能在下面的表达式中获得值click_type,因为我想在脚本的后期比较它?

cc.on_button_single_or_double_click_or_hold = lambda channel, click_type, was_queued, time_diff: \
        print(click_type) 

1 个答案:

答案 0 :(得分:0)

您可以使用lambda的任何地方,也可以定义一个函数。那怎么样?

def record_click(click_type=None):
    """
    Save each click seen in a list
    """
    try:
        cache = record_click.cache 
    except AttributeError:
        record_click.cache = []
    if click_type is not None:
        cache.append(click_type)
    return cache

def handle_click(channel, click_type, was_queued, time_diff):
    """
    Report each click, then record it for later
    """
    print(click_type)
    record_click(click_type)

cc.on_button_single_or_double_click_or_hold = handle_click

代码中的其他地方:

def foobar(...):
    """
    Look at the most recent click
    """
    click_list = record_click()
    if click_list[-1] == ???:
        do something

函数record_click存储一个点击列表。当你没有参数调用它时,它会将列表返回给你。 click_list [-1]获取最近的点击,列表中的最后一次点击。