Home > Mobile >  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

Time:05-31

I have declared lists in the global space of a python class. I import it into another class that uses Arcpy, and I get the error: global name 'targetFieldNames' is not defined. I have tried initializing the list with a value (targetFieldNames = ['junk']). I have tried putting all the global lists in an __init__. When I comment out the targetFieldNames, the error switches to my dBug = DBug() line.

Here is the code:

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 )

CodePudding user response:

I’m pretty sure using self should fix your issue

Would this work for you?

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 )
  • Related