Python声明的全局类列表正在创建错误:未定义的全局名称...未定义

发布于 2025-02-02 17:09:58 字数 1371 浏览 2 评论 0原文

我在Python类的全球空间中宣布了列表。我将其导入另一个使用Arcpy的类,并且我会收到错误:未定义的全局名称'TargetFieldNames'。我尝试使用值(TargetFieldNames = ['junk'])初始化列表。我已经尝试将所有全局列表放入__ INIT __。时,当我评论targetFieldNames时,错误会切换到我的dbug = dbug() line。

这是代码:

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 )

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 )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往日 2025-02-09 17:09:58

我很确定使用自己应该解决您的问题,

这对您有用吗?

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 )

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 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文