jborgion

Passing Variables between Defs

Discussion created by jborgion Champion on Jul 17, 2019
Latest reply on Jul 18, 2019 by bixb0012

Global variables seem to draw the scorn if not ire of most python developers, and recently I've been faced with a situation where creating a variable in a series of defs needs to be passed back to  main().  I did a little searching with my friend and associate Dr. Google and found a stack over flow thread which gave me an idea. 

 

In the script I'm (re) writing, I'd like to follow the form of defining my defs() and then calling them from main().  However, each of my defs() use a try/except block, and if there is an exception anywhere along the line I need to abort the final processing.

 

This is what I came up with in concept:

errorList = []
def first():
    try:
        #some code
    except:
        errorList.append(1)

def second():
    try:
        #some other code
    except:
        errorList.append(1)
   
def main():
    first()
    second()
    if len(errorList) == 0:
        # final operations
    else:
        # don't execute final operations   
   
main()

Pretty simple concept right?

 

Taking it one step further, I started thinking how about this:

def1List = []
def2List = []

def one():
    #blah,blah
    append def1List(someValueFromBlah)
    append def1List(anotherValueFromBlah)

def two():
    # blah2 blah2
    append def2List(someValueFromBlah2)
    append def2List(anotherValueFromBlah2)
   
def three():
    if def1List[0] == myTargetValue:
        do something
    elif defList[1] == myOtherTargetValue:
        do something else...
    myValue = def2List[0]
    myOtherValue = def2List[1]
    # use myValue and MyOtherValue as I need to...

 

These look like they'll work for me in a number of different applications, but I'd like to here from you (and you know who you are) as to whether or not these are:

 

Good Practice

Bad Practice

Something everybody does already

Etc

Etc....

Outcomes