Python variable assigned by an outside module is accessible for printing but not for assignment in the target module

后端 未结 2 430
春和景丽
春和景丽 2021-01-24 17:42

I have two files, one is in the webroot, and another is a bootstrap located one folder above the web root (this is CGI programming by the way).

The index file in the web

相关标签:
2条回答
  • 2021-01-24 18:23

    try this:

    
    def initialize():
        global VAR
        print('Content-type: text/html\n\n')
        print(VAR)
        VAR = 'h'
        print(VAR)
    
    

    Without 'global VAR' python want to use local variable VAR and give you "UnboundLocalError: local variable 'VAR' referenced before assignment"

    0 讨论(0)
  • 2021-01-24 18:48

    Don't declare it global, pass it instead and return it if you need to have a new value, like this:

    def initialize(a):
        print('Content-type: text/html\n\n')
        print a
        return 'h'
    
    ----
    
    import bootstrap
    b = bootstrap.initialize('testVar')
    
    0 讨论(0)
提交回复
热议问题