繁体   English   中英

Python:将额外的参数传递给Callable

[英]Python: Pass extra arguments to callable

我有以下python代码:

import networkx as nx 

def cost(i, j, d, value1, value2):
    # some operation involving all these values
    return cost


# graph is a networkx graph
# src, dst are integers
# cost is a callable that is passed 3 values automatically i.e. src, dst and d 
# d is a dictionary of edge weights
path = nx.dijkstra_path(graph, src, dst, weight=cost)

现在,我想将两个值value1value2传递给cost函数。

networkx文档说, weight可以是可调用的,它恰好接受3个参数。 但是我需要value1value2进行计算。 如何才能做到这一点?

编辑使用functools解决方案效果很好。 但是,我的函数在一个类中,如下所示:

import networkx as nx 
import functools
class Myclass:
    def cost(self, i, j, d, value2):
        # some operation involving all these values
        # also need self


    # graph is a networkx graph
    # src, dst are integers
    # cost is a callable that is passed 3 values automatically i.e. src, dst and d 
    # d is a dictionary of edge weights
    # path = nx.dijkstra_path(graph, src, dst, cost)
    cost_partial = functools.partial(cost, self=self, value2=5)
    path = nx.dijkstra_path(graph, src, dst, cost_partial)

使用这种方法, nx.dijkstra_path坚持将src分配给self 因此,解释器抱怨self被分配了多个值。 我需要自我来计算成本。

您只需要一个包装cost的函数。 一种快速的方法是使用functools.partial

import functools

def cost(i, j, d, value1, value2):
    'Example function'
    return i, j, d, value1, value2

# This makes a new function which takes all parameters of `cost` except those 
# you've already passed. Here I'm going to set value1 and value2 to 1 and 2 
# respectively.
cost_partial = functools.partial(cost, value1=1, value2=2)

# A new function that only accepts the 3 args i, j, d
cost_partial('i', 'j', 'd')  # --> ('i', 'j', 'd', 1, 2)

# use as
path = nx.dijkstra_path(graph, src, dst, weight=cost_partial)

这在很大程度上取决于value1value2的含义。 我建议添加一个可由networkx调用的包装器:

def cost_wrapper(i, j, d):
    value1 = 0  # set values as needed
    value2 = 1
    return cost(i, j, d, value1, value2)

并将其提供给networkx:

path = nx.dijkstra_path(graph, src, dst, weight=cost_wrapper)

或者只是使它们成为全局变量,而不是参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM