0

I am declaring a global list, and as part of the list element definition, I need to reference some obscure data from other modules. I would like to declare some simple short-hand references to these external variables right before declaring the list, but I don't want these reference variables to become global variables.

This all probably sounds very confusing, so here is an example:

prj = book.OverrideLevel.PROJECT
ent = book.OverrideLevel.ENTITY
elm = book.OverrideLevel.ELEMENT

list = [OvrType( 3, prj, "OutTypes" ),
        OvrType( 4, prj, "FilePrefix" ),
        OvrType( 5, ent, "FileSuffix" ),
        OvrType( 6, ent, "World" ),
        OvrType( 7, ent, "Anim" ),
        OvrType( 8, ent, "Armature" ),
        OvrType( 9, ent, "MeshData" ),
        OvrType( 10, elm, "General" ),
        OvrType( 11, elm, "ObjPrefix" ),
        OvrType( 12, elm, "ObjSuffix" ) ]

In the example, I want to avoid defining any globals other than list. Since this is happening in the global scope, prj, ent, and elm will also become global variables. Is there any way to avoid this in Python? Perhaps by defining the list as empty (list = []), then creating a +1 scope, and defining the list contents there? I'm just not sure how to do this.

One thing I've considered is to create a function, then call it immediately:

list = []
def BuildList():

    prj = book.OverrideLevel.PROJECT
    ent = book.OverrideLevel.ENTITY
    elm = book.OverrideLevel.ELEMENT

    global list    
    list = [OvrType( 3, prj, "OutTypes" ),
            .... ]

BuildList()

Would there be a better way? I know some languages allow you to add scopes manually for any reason needed. But since Python is so text-driven (spacing=scope), I wasn't sure if something like that would be permitted.

I appreciate any advice

Robert
  • 413
  • 4
  • 12

1 Answers1

1

One way to do it would be to delete the variables from the global namespace when you no longer need them:

prj = book.OverrideLevel.PROJECT
ent = book.OverrideLevel.ENTITY
elm = book.OverrideLevel.ELEMENT

list = [OvrType( 3, prj, "OutTypes" ),
        OvrType( 4, prj, "FilePrefix" ),
        ...
       ]

del prj, ent, elm
chash
  • 3,975
  • 13
  • 29