为什么我需要将导入声明为全局函数 [英] Why I need to declare import as global in function

查看:96
本文介绍了为什么我需要将导入声明为全局函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目有一个巨大的副作用:


在我的项目中我需要写gobal使用全球符号:


....

导入数学

....

def f():

全球数学#必要??????否则下一行会产生错误

留言?????

打印math.pi


(问题适用于所有人全局模块符号)


我的项目肯定会改变,但我找不到什么?


(只是一个小的)当然没有这种全球性的程序麦芽汁很好)


任何人都可以帮助我:我在哪里可以搜索项目中的错误?

I have a stange side effect in my project :

in my project I need to write "gobal" to use global symbol :

....
import math
....
def f() :
global math # necessary ?????? else next line generate an error
message ?????
print math.pi

(the problem is for all global module symbol)

I have certainly change somthing in my project, but I can''t find what ?

(just a small program wort fine without this global of course)

Can anybody help me : where can I search the mistake in my project ?

推荐答案

听起来像是某种东西,无论是在您的程序中,还是在另一个lib中你导入的b
,或者你最近安装的某个扩展程序(以及哪些

自动启动),覆盖''import''(用它自己的

版本替换它) - 并忘记将导入的模块正确添加到

globlals?

或者其他什么选项可能会改变Python

识别全局变量的方式?


如果你声明另一个全局变量,然后尝试使用它在你的

函数中,那么结果是什么?


你用什么Python版本?


欢呼,

--Tim

Sounds like something, either in your program, in another lib you
imported, or perhaps some extension you recently installed (and which
automatically starts), overrides ''import'' (replaces it with it''s own
version) -- and forgets to add the imported modules properly to the
globlals?
Or something, some option, that perhaps changes the way that Python
recognizes globals?

If you declare another global variable, then try to use it in your
function, then what''s the result?

What Python version do you use?

cheers,

--Tim


di ************* @ gmail.com 写道:
di*************@gmail.com wrote:
我的项目有一个巨大的副作用:

在我的项目中我需要写gobal。使用全局符号:

...导入数学
...
def f():
全局数学#必要???? ??否则下一行会产生错误
消息?????


什么错误信息?

打印math.pi
I have a stange side effect in my project :

in my project I need to write "gobal" to use global symbol :

...
import math
...
def f() :
global math # necessary ?????? else next line generate an error
message ?????
what error message?
print math.pi




你只需要全局如果你在某个地方指定名称

稍后在函数中(例如math = ...或import math或其他一些

赋值)


请发布错误信息(整个追溯)。


< / F>



you only need global in this case if you assign to the name somewhere
later in the function (e.g. "math = ..." or "import math" or some other
assignment)

please post the error message (the entire traceback).

</F>


错误信息:


EXCEPTION RAISED ::


Traceback(最近一次调用最后一次):

文件" ../ tu.py",第21行,在run_tu中

execfile(文件名)

文件" TU_05_010.py" ,第8行,在?

导入TU_05_tools

文件" ./ TU_05_tools.py",第4行,在?

f()

文件" ./ TU_05_tools.py",第2行,在f

打印math.pi

NameError:全局名称''math' '没有定义


我已经知道这个问题是rai当我在

导入的模块中执行代码时(在导入期间),我认为我将能够隔离它并很快得到一个简单的样本。 ..

the error message :

EXCEPTION RAISED::

Traceback (most recent call last):
File "../tu.py", line 21, in run_tu
execfile( filename )
File "TU_05_010.py", line 8, in ?
import TU_05_tools
File "./TU_05_tools.py", line 4, in ?
f()
File "./TU_05_tools.py", line 2, in f
print math.pi
NameError: global name ''math'' is not defined

I have remarq that this problem is raised when I execute code in an
imported module (during importation)

I think I will be able to isolate it and have a simple sample soon ....


这篇关于为什么我需要将导入声明为全局函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆