Home  >  Q&A  >  body text

python 全局变量

python 中的全局变量是什么概念?是在一个模块中定义的,它的作用域是什么?在一个文件中定义的全局变量,在其它的文件中要使用那个变量要怎么使用呢?
有没有那种像C语言的中的全局变量一样,然后在整个程序中都可以用的?

PHP中文网PHP中文网2741 days ago469

reply all(1)I'll reply

  • PHPz

    PHPz2017-04-17 17:51:55

    If you want to use C language, you have to declare it as extern. Python just needs to be imported.

    a.py:
    AAA='AAA'
    
    b.py:
    from a import AAA
    print AAA
    AAA='123'

    By default, the scope is only the file that defines it. To use other files, you need to import

    reply
    0
  • Cancelreply