【问题标题】:UnboundLocalError with Python on ArcGis在 ArcGis 上使用 Python 的 UnboundLocalError
【发布时间】:2013-08-13 16:56:36
【问题描述】:

我正在研究 ArcMap,我有这个 python 代码:

import arcpy, sys

feature = arcpy.GetParameterAsText(0)


def nearRoutine():
    #calculate the distances using the current dataset
    arcpy.Near_analysis(feature, feature)

    #iterate through any features which are within the distance
    cur = arcpy.UpdateCursor(feature, '"NEAR_DIST" < 500')
    row1 = cur.next()
    while row1:

        #this point is within the distance of its neighbor, so delete it
        cur.deleteRow(row1)

        #now re-run this routine on the new dataset
        del row1, cur
        nearRoutine

#call the recursive routine. It will get progressively faster to run as it will loop through fewer points each time
nearRoutine()

我的错误信息:UnboundLocalError: local variable 'row1' referenced before assignment

我不明白,因为我的变量已明确定义...

有人有问题吗?

【问题讨论】:

  • 欢迎来到 Stack Overflow!感谢您提供代码示例以及您的问题。我很好奇你希望在这段代码中发生什么:while row1: ... ; del row1 ...
  • 确保我的变量已被删除,然后再重新启动我的订单 nearRoutine
  • 我明白了。递归调用nearRoutine 与其说是restart 函数,不如说是创建它的一个新实例。局部变量对于函数的每个实例都是唯一的;内部函数框架中的“row1”与外部函数框架中的“row1”完全无关,因此不需要del。而且,正如您所发现的那样,由于其他原因很糟糕。

标签: python arcgis


【解决方案1】:

您删除row1,然后继续迭代要求(检查)它!

我不确定您是否真的需要在变量 row1cur 上使用 del。您正在删除这些变量,而不是它们在数据结构中的内容。

【讨论】:

  • 我重新定义了我删除的两个变量(row1 和 cur),它的工作谢谢
  • 很高兴我能提供帮助。请务必点击分数旁边的复选标记accept此答案。
猜你喜欢
  • 2015-09-22
  • 1970-01-01
  • 2015-04-01
  • 2012-11-03
  • 1970-01-01
  • 2021-12-01
  • 1970-01-01
  • 2013-11-27
  • 2012-12-25
相关资源
最近更新 更多