【问题标题】:Confused about function对功能感到困惑
【发布时间】:2020-05-25 03:10:03
【问题描述】:

我正在开始制作一个函数,这是我现在的代码:

def shortest_path(source, target):

    frontier_d = {}

    frontier_d[source]=1


    raise NotImplementedError

基本上,我正在尝试创建一个字典并将源(函数的输入)作为与值 1 关联的键。但是,Pycharm 告诉我源是一个未解决的引用。如果 source 是函数的输入,为什么会发生这种情况?

【问题讨论】:

  • 错误的截图或完整输出会很好
  • 请将此减少并增强为预期的MRE
  • 您没有显示调用函数的位置,问题可能就在那里。您可以添加一个额外的行来调用shortest_path 并包含您在运行它时获得的任何回溯吗?此外,加注会混淆这个测试的问题。最好拿出来。

标签: python function pycharm


【解决方案1】:

看看这个—— When to use 'raise NotImplementedError'?

source="abc"
target=156
def shortest_path(source, target):
    try:
        frontier_d = {}
        frontier_d[source]=target
        print(frontier_d)
    except:
        raise NotImplementedError
shortest_path(source, target)

输出:{'abc': 156}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 2013-08-21
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多