Help Defining Global Names

后端 未结 6 436
情深已故
情深已故 2021-01-25 22:48

My Code:

def A():
    a = \'A\'

    print a

    return

def B():

    print a + \' in B\'

    return

When B() is entered into the interpeter

6条回答
  •  走了就别回头了
    2021-01-25 23:42

    check out my answer from this SO question. Basically:

    Create a new module containing only global data (in your case let's say myGlobals.py):

    # create an instance of some data you want to share across modules
    a=0
    

    and then each file you want to have access to this data can do so in this fashion:

    import myGlobals
    
    myGlobals.a = 'something'
    

    so in your case:

    import myGlobals
    
    def A():
        myGlobals.a = 'A'
        print myGlobals.a
    
    def B():
        print myGlobals.a + ' in B'
    

提交回复
热议问题