Help Defining Global Names

后端 未结 6 566
情深已故
情深已故 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:46

    You can do this by using the global keyword:

    def A():
        global a
        a = 'A'
    
    def B():
        global a
        # ...
    

    However, using global variables is generally a bad idea - are you sure there's not a better way to do what you want to do?

提交回复
热议问题