Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 从函数定义模块变量_Function_Variables_Python 3.x_Scope_Flake8 - Fatal编程技术网

Function 从函数定义模块变量

Function 从函数定义模块变量,function,variables,python-3.x,scope,flake8,Function,Variables,Python 3.x,Scope,Flake8,我终于开始接触Python,并注意到一些奇怪的事情,在Java中工作,但在Python中不工作 当我键入以下内容时: fn = "" # Local filename storage. def read(filename): fn = filename return open(filename, 'r').read() 我的用于Atom的Flake 8 linter出现以下错误: F841-局部变量“fn”已分配给但从未使用过 我假设这意味着变量是在def级别上定义的,而不

我终于开始接触Python,并注意到一些奇怪的事情,在Java中工作,但在Python中不工作

当我键入以下内容时:

fn = ""  # Local filename storage.

def read(filename):
    fn = filename
    return open(filename, 'r').read()
我的用于Atom的Flake 8 linter出现以下错误:

F841-局部变量“fn”已分配给但从未使用过

我假设这意味着变量是在def级别上定义的,而不是我打算在模块级别上定义的。如果我错了,请纠正我

我搜索过谷歌,有多个词,但似乎无法以正确的结果显示的方式对其进行命名…

关于如何从功能级别实现模块级变量定义的任何想法?

如果要将
fn
声明为全局变量(模块级),请使用

def读取(文件名):

全局fn#您可以通过以下操作从函数中设置模块级变量:

import sys

def read(filename):        
    module = sys.modules[__name__]
    setattr(module, 'fn', filename)
    return open(filename, 'r').read()
然而,这是一种非常奇怪的需要。考虑改变你的体系结构。< /P>

<强> UPD: 让我们考虑一个例子:

# module1
# uncomment it to fix NameError and AttributeError
# some_var = '' 

def foo(val):
    global some_var
    some_var = val


# module2
from module1 import *
print(some_var)  # raises NameError: name 'some_var' is not defined

foo('bar')
print(some_var)  # still raises NameError: name 'some_var' is not defined


# module3
import module1
print(module1.some_var)  # raises AttributeError: 'module' object has no attribute 'some_var'

foo('bar')
print(module1.some_var)  # prints 'bar' even without some_var = '' definition in the module1

因此,
global
在导入过程中的行为并不明显。我认为,在
read()
调用期间手动执行
setattr(模块'attr\u name',value)
更为清晰。

fn
已分配,但代码使用的不是
fn
,而是
filename
。为什么不使用
fn
return open(fn,'r').read()
@falsetru我正在从另一个类访问文件级
fn
。它的存储以备以后使用。然后,您需要将它声明为全局
全局fn
@falsetru那么如何从函数范围中定义它?;)那好吧。我试试这个。顺便说一句,当调用从io导入*
时,从变量级别对其进行全局化是否会自动导入它?@frayment,是的,除非您定义
\uuuuuuu all\uuuuu
来手动控制它。请参见,要在导入后从其他模块访问
fn
,您不仅必须在
read()
函数中将其定义为
global
,还必须在模块级别上定义它(例如
fn='
)。如果没有这样的定义,在从io导入*
执行
之后和调用
read()之前,您将无法访问
fn
。您的答案与其他答案在功能和底层工作方面的主要区别是什么?通过这种方法,您可以在执行
读取
功能的过程中设置变量,而不仅仅是对定义它的模块进行设置。同时,导入期间的任何代码执行都会导致非常糟糕的可测试体系结构。尽量避免。通常在测试过程中,您不能忽略导入,但是,您需要对某些代码进行模拟/存根。这与全局定义之间的区别是什么?@frayment,实际上使用
global fn
并手动将
fn
属性设置为
read
的模块。嗯。。。很难做出决定,但我会给你投票权,尽管我用我的分歧问题刺激了你。现在,我想我会坚持使用globals,但看到双方都解释得很好真是太好了。谢谢
# module1
# uncomment it to fix NameError and AttributeError
# some_var = '' 

def foo(val):
    global some_var
    some_var = val


# module2
from module1 import *
print(some_var)  # raises NameError: name 'some_var' is not defined

foo('bar')
print(some_var)  # still raises NameError: name 'some_var' is not defined


# module3
import module1
print(module1.some_var)  # raises AttributeError: 'module' object has no attribute 'some_var'

foo('bar')
print(module1.some_var)  # prints 'bar' even without some_var = '' definition in the module1