Python Declared Global Class List is Creating the Error: Unknown global name...is not defined

Python Declared Global Class List is Creating the Error: Unknown global name...is not defined

我已经在 python class 的全局 space 中声明了列表。我将其导入到另一个使用 Arcpy 的 class 中,但出现错误:未定义全局名称 'targetFieldNames'。我尝试用值 (targetFieldNames = ['junk']) 初始化列表。我尝试将所有全局列表放在 __init__. 中 当我注释掉 targetFieldNames 时,错误切换到我的 dBug = DBug() 行。

代码如下:

import arcpy
from dbug import DBug # My own debug class


class FieldMill:

    # Set up the class level stuffs
    baseFieldNames = []
    targetFieldNames = []
    fieldsToCorrect = []

    # Call in our own logger
    dBug = DBug()


    # Separate the list into two: the base (properly named) fields
    # and the target (suspectly named) fields.
    def make_cases_match( self, fieldList ):
        for f in fieldList:
            if not f.required:
                if f.name.endswith( '_1' ):
                    #baseFields.append( f )
                    strippedName = f.name.replace( '_1', "" )   # Can't match with '_1'
                    baseFieldNames.append( strippedName )
                else:
                    #targetField.append( f )
                    targetFieldNames.append( f.name )
        
            # These lines added for debug
        dBug.printMessage("\n##### HERE IS WHAT baseFieldNames GOT:")
        dBug.printMessage( baseFieldNames )
        dBug.printMessage("\n##### HERE IS WHAT targetFieldNames GOT:")
        dBug.printMessage( targetFieldNames )

我很确定使用 self 应该可以解决您的问题

这对你有用吗?

import arcpy
from dbug import DBug # My own debug class


class FieldMill:

    def __init__(self):
        # Set up the class level stuffs
        self.baseFieldNames = []
        self.targetFieldNames = []
        self.fieldsToCorrect = []

        # Call in our own logger
        self.dBug = DBug()


    # Separate the list into two: the base (properly named) fields
    # and the target (suspectly named) fields.
    def make_cases_match( self, fieldList ):
        for f in fieldList:
            if not f.required:
                if f.name.endswith( '_1' ):
                    #baseFields.append( f )
                    strippedName = f.name.replace( '_1', "" )   # Can't match with '_1'
                    self.baseFieldNames.append( strippedName )
                else:
                    #targetField.append( f )
                    self.targetFieldNames.append( f.name )
        
            # These lines added for debug
        self.dBug.printMessage("\n##### HERE IS WHAT baseFieldNames GOT:")
        self.dBug.printMessage( self.baseFieldNames )
        self.dBug.printMessage("\n##### HERE IS WHAT targetFieldNames GOT:")
        self.dBug.printMessage( self.targetFieldNames )