如何在Python中使用外部变量,比如'extern int x ';在C中?

时间:2022-09-07 21:04:04

How can I use external variables in Python, like extern int x; in C?

如何在Python中使用外部变量,比如extern int x;在C语言中?

For example,

例如,

main1.py:

main1.py:

from myfunc import print_a 

a = 10 
print a    
print_a()

myfunc.py:

myfunc.py:

def print_a():
    global a   
    print a

2 个解决方案

#1


6  

Simply re-assign the variable in the module:

简单地重新分配模块中的变量:

import myfunc
from myfunc import print_a 

a = 10 

print a

myfunc.a = a

print_a()

Otherwise it is not possible.

否则这是不可能的。

Rememeber that python treats modules in a way that is quite different from C. The import in python does not "copy the contents" of the file in that place, but it executes the code in the given file and creates a module object.

记住,python处理模块的方式与c完全不同。

The global variable of the module are the module object attributes, which can be modified as I've shown. There is no such notion as "global variable" except for built-ins.

模块的全局变量是模块对象属性,可以像我展示的那样进行修改。除了内置变量外,没有“全局变量”这样的概念。


I'd suggest to refactor your code in such a way that you don't have to modify this global variable at all, moving the code that uses myfunc.a from main1 to myfunc. The fact that you need such global variable is already a code smell that there's something wrong with your code and you should try to fix it.

我建议重构您的代码,使您不必修改这个全局变量,移动使用myfunc的代码。从main1到myfunc。您需要这样的全局变量,这已经是一种代码味道,您的代码有问题,您应该尝试修复它。


Actually there is a way to affect the "global scope" but it is so hackish that I don't even want to mention it. Trust me: you don't want to use it. If people see your code using such a hack you may be in physical danger.

事实上,有一种方法可以影响“全球范围”,但它太陈腐了,我甚至不想提它。相信我:你不想用它。如果人们看到你的代码使用这样的黑客,你可能处于身体危险。

#2


1  

Unlike C, variables declared at global scope are still limited in scope to the module they are created in, so you need to qualify the name a with the module it lives in.

与C不同的是,全局作用域声明的变量在作用域上仍然局限于它们所创建的模块,因此您需要用它所在的模块限定名称a。

The global keyword is used when you are going to modify a global variable by reassigning, you do not need it when you are just referencing a global variable.

全局关键字用于通过重新分配来修改全局变量时,在引用全局变量时不需要它。

If you are trying to access a variable of another file, you must import that module, and because of the way your files are structured you have a couple of ways to resolve issues:

如果您试图访问另一个文件的变量,您必须导入该模块,并且由于您的文件的结构方式,您有两种方法来解决问题:

Option 1) Move the referencing of myfunc.print_a inside of a function and import main1 inside myfunc to see a

选项1)移动myfunc的引用。print_a在函数内部,并在myfunc中导入main1,以查看a。

main1.py

main1.py

import myfunc

a = 10 

def main():
    print a    
    myfunc.print_a()

if __name__ == '__main__':
    main()

myfunc.py

myfunc.py

import main1

def print_a():
    print main1.a

Option 2) recommended Move the variable(s) into another module and have both myfunc and main1 import it.

选项2)建议将变量移动到另一个模块,并让myfunc和main1都导入它。

vals.py

vals.py

a = 20

main1.py

main1.py

import vals
from myfunc import print_a

vals.a = 10 

print vals.a    
print_a()

myfunc.py

myfunc.py

import vals

def print_a():
    print vals.a

#1


6  

Simply re-assign the variable in the module:

简单地重新分配模块中的变量:

import myfunc
from myfunc import print_a 

a = 10 

print a

myfunc.a = a

print_a()

Otherwise it is not possible.

否则这是不可能的。

Rememeber that python treats modules in a way that is quite different from C. The import in python does not "copy the contents" of the file in that place, but it executes the code in the given file and creates a module object.

记住,python处理模块的方式与c完全不同。

The global variable of the module are the module object attributes, which can be modified as I've shown. There is no such notion as "global variable" except for built-ins.

模块的全局变量是模块对象属性,可以像我展示的那样进行修改。除了内置变量外,没有“全局变量”这样的概念。


I'd suggest to refactor your code in such a way that you don't have to modify this global variable at all, moving the code that uses myfunc.a from main1 to myfunc. The fact that you need such global variable is already a code smell that there's something wrong with your code and you should try to fix it.

我建议重构您的代码,使您不必修改这个全局变量,移动使用myfunc的代码。从main1到myfunc。您需要这样的全局变量,这已经是一种代码味道,您的代码有问题,您应该尝试修复它。


Actually there is a way to affect the "global scope" but it is so hackish that I don't even want to mention it. Trust me: you don't want to use it. If people see your code using such a hack you may be in physical danger.

事实上,有一种方法可以影响“全球范围”,但它太陈腐了,我甚至不想提它。相信我:你不想用它。如果人们看到你的代码使用这样的黑客,你可能处于身体危险。

#2


1  

Unlike C, variables declared at global scope are still limited in scope to the module they are created in, so you need to qualify the name a with the module it lives in.

与C不同的是,全局作用域声明的变量在作用域上仍然局限于它们所创建的模块,因此您需要用它所在的模块限定名称a。

The global keyword is used when you are going to modify a global variable by reassigning, you do not need it when you are just referencing a global variable.

全局关键字用于通过重新分配来修改全局变量时,在引用全局变量时不需要它。

If you are trying to access a variable of another file, you must import that module, and because of the way your files are structured you have a couple of ways to resolve issues:

如果您试图访问另一个文件的变量,您必须导入该模块,并且由于您的文件的结构方式,您有两种方法来解决问题:

Option 1) Move the referencing of myfunc.print_a inside of a function and import main1 inside myfunc to see a

选项1)移动myfunc的引用。print_a在函数内部,并在myfunc中导入main1,以查看a。

main1.py

main1.py

import myfunc

a = 10 

def main():
    print a    
    myfunc.print_a()

if __name__ == '__main__':
    main()

myfunc.py

myfunc.py

import main1

def print_a():
    print main1.a

Option 2) recommended Move the variable(s) into another module and have both myfunc and main1 import it.

选项2)建议将变量移动到另一个模块,并让myfunc和main1都导入它。

vals.py

vals.py

a = 20

main1.py

main1.py

import vals
from myfunc import print_a

vals.a = 10 

print vals.a    
print_a()

myfunc.py

myfunc.py

import vals

def print_a():
    print vals.a