简体   繁体   English

在Python中更改布尔函数的值

[英]Changing the value of a Boolean Function in Python

I want to set the default value of a boolean function to be False and want to change it to True only for certain values of the input in between the code. 我想将布尔函数的默认值设置为False,并且只想将代码之间的输入的某些值更改为True。 Is there a way to do it? 有办法吗?

I'm trying to write a simple DFS search code. 我正在尝试编写一个简单的DFS搜索代码。 The code I'm using is this: 我正在使用的代码是这样的:

def visited(v):
    return False
def explore(v):
    visited(v) = True
    for (v,w) in E:
        if not visited(w):
            explore(w)

A function is probably the wrong tool here. 函数在这里可能是错误的工具。 Instead, try a set: 而是尝试一组:

def explore(v, visited=set()):
    visited.add(v)
    for (v,w) in E:
        if w not in visited:
            explore(w)

I'm using a sometimes unintuitive behavior of default arguments in Python for this example code because it's convenient, but you could also use a different way of maintaining a shared set, such as a wrapper function that initializes a blank set and then calls a recursive helper function. 对于此示例代码,我在Python中使用默认参数的有时不直观的行为,因为它很方便,但是您也可以使用其他方式来维护共享集,例如,包装函数初始化一个空白集,然后调用递归辅助功能。 (That would let you explore multiple times by resetting the set each time.) (这将使您可以通过每次重置设置来进行多次探索。)

No, you cannot set the return value of a function from outside the function. 不可以,您不能从函数外部设置函数的返回值。 Instead, use a variable in the calling function. 而是在调用函数中使用变量。

For instance, here, you want to remember which nodes you visited. 例如,在这里,您想记住您访问过哪些节点。 A set is good for remembering a set of objects. set有助于记住一组对象。

def explore(v):
    visited.add(v)
    for (v,w) in E:
        if w not in visited:
            explore(w)

A couple of cautions about this: 关于此的一些注意事项:

If you call it twice, everything will be seen as already visited, because the state is tracked in a global. 如果您两次调用它,那么所有内容都将被视为已访问,因为该状态是在全局状态中跟踪的。 That's similar to what you already have but may or may not be what you want. 这与您已经拥有的东西相似,但可能不是您想要的。 If you want to be able to iterate twice, you need to pass this down as a parameter, and preferably add a second function that starts the recursion: 如果您希望能够迭代两次,则需要将其作为参数传递,最好添加第二个函数来启动递归:

def explore(v):
    return explore_down(v, set())

def explore_down(v, visited):
    visited.add(v)
    for (v,w) in E:
        if w not in visited:
            explore(w)

Also, depending on what type v and w are, you may get an error that they're not hashable, for which see this question. 另外,根据vw类型,您可能会得到一个错误,即它们不可散列,有关此问题,请参见。

Assuming you have a myfunc function returning a boolean, that you want to modify the behaviour: 假设您有一个myfunc函数返回一个布尔值,那么您想要修改行为:

_myfunc = myfunc

def myfunc(*args):
    if some_condition:
        _myfunc(*args)
    else:
        return False

This way, you will trigger the actual function only in wished cases. 这样,您仅在希望的情况下才触发实际功能。

This solution overwrites the original name, but you are not obliged to do so. 此解决方案将覆盖原始名称,但您没有义务这样做。

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

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