global,globals(),_ global? [英] global, globals(), _global ?
问题描述
在Python中使用全局变量通常会引起混乱。其他语言使用
a清除前缀为全局。
*你忘了申报全局
*或者你宣布全局很多或有冲突
*你有一个本地相同的变量名称,并希望保存/加载它
来自/来自同名的全球
*当你添加代码时,全局变量的定义越来越多,除了它们的用例之外,它们的移动越来越多 - >怪事;程序员思维分散
*使用globals()[''xy'']是''stringy non-program-code''
因此,自从很久以来我经常把更大模块的负责人......
_global = sys.modules [__ name __]
....并使用全局仅变量/主要像
def f(y):
v = x = _global.x
_global.y = y
...
对我来说这个方法整体来说要清晰得多。并且它与自我的明确阐述是一致的。作为Python方法中的常规对象(与其他
语言中的冻结@ $ m _ ...。hacks相比,更加自我类似的方式)
我知道,这个家创建_global创建一个循环引用,但通常
这并不严重,因为模块只在程序结束时卸载。
(旁边的问题:这是否可能阻碍全局对象正确地运行;当所有模块被拉动时,至少gc()运行
退出())
不知怎的,我错过了一个很好的标准方法,可以在
未分段的方式使用全局变量。你觉得怎么样?
罗伯特
Using global variables in Python often raises chaos. Other languages use
a clear prefix for globals.
* you forget to declare a global
* or you declare a global too much or in conflict
* you have a local identical variable name and want to save/load it
to/from the global with same name
* while you add code, the definition of globals moves more and more
apart from their use cases -> weirdness; programmers thinking is fragmented
* using globals()[''xy''] is ''stringy non-program-code''
Thus, since long time at the head of my bigger modules I often put...
_global = sys.modules[__name__]
....and use global variables only/mainly like
def f(y):
v=x=_global.x
_global.y=y
...
for me this method is much more clear overall. And it is in line with
the clear exposition of "self" as regular object in Python methods (in a
much more self-similiar way compared to frozen @ $ m_... hacks in other
languages)
I know, this home created _global creates a circular ref, but usually
this is not serious as modules are unload only at the end of a program.
( side question: does this maybe hinder global objects from being
__del__''ed correctly; is at least gc() run when all modules are pulled
off at exit() )
Somehow I miss a nice standard method for using globals in an
unfragmented way everywhere. What do you think?
Robert
推荐答案
m _... hacks in other
语言)
我知道,这个家创建_global创建一个循环引用,但通常
这并不严重,因为模块只卸载一个程序的结束。
(旁边的问题:这是否可能阻碍全局对象正确地使用?b $ b __del __''正确;至少是gc()拉出所有模块时运行
退出退出())
不知怎的,我错过了一个很好的标准方法,可以在
$ b中使用全局变量到处都是$ b未分段的方式。你怎么看?
罗伯特
m_... hacks in other
languages)
I know, this home created _global creates a circular ref, but usually
this is not serious as modules are unload only at the end of a program.
( side question: does this maybe hinder global objects from being
__del__''ed correctly; is at least gc() run when all modules are pulled
off at exit() )
Somehow I miss a nice standard method for using globals in an
unfragmented way everywhere. What do you think?
Robert
嗨罗伯特,
我是前段时间也使用全局变量。
但是程序的时间根本不可维护,因为它非常好b / b很难
跟踪,为什么一个全局变量有一些特殊的价值,而不是那个,你认为它应该有b $ b。
所以我重新设计了程序,现在我可以在没有全局变量的情况下完成它。
对我来说,全球变量现在在大多数情况下都是设计糟糕的标志。
有没有其他方法可以做到这一点?
Greets
Xaver
" robert" <无***** @ no-spam-no-spam.com> schrieb im Newsbeitrag
news:dv *********** @ ulysses.news.tiscali.de ...
Hi Robert,
I was using global variables some time ago, too.
But with the time the program simply got unmaintainable, because it is very
hard
to trace, why a global variable has some special value and not the one, you
thought it should have.
So I redesigned the program and now I can do it without global variables.
To me global variables are now in most cases a sign of bad design.
Is there no other way to do it?
Greets
Xaver
"robert" <no*****@no-spam-no-spam.com> schrieb im Newsbeitrag
news:dv***********@ulysses.news.tiscali.de...
经常在Python中使用全局变量引起混乱。其他语言使用
清除前缀用于全局。
*你忘记宣布全局
*或者你声明全球太多或冲突
*你有一个本地相同的变量名称,并希望将其保存/加载到具有相同名称的全局
*当您添加代码时,全局变量的定义越来越多地与它们的使用分开案例 - >怪事;程序员思维分散
*使用globals()[''xy'']是''拉链非程序代码''
因此,因为很长一段时间在我的头脑中我常常放的模块......
_global = sys.modules [__ name __]
...并且仅使用全局变量/主要像
def f(y):
v = x = _global.x
_global.y = y
...
对我来说这种方法整体要清晰得多。这与自我的明确阐述是一致的。作为Python方法中的常规对象(与其他
语言中的冻结@
Using global variables in Python often raises chaos. Other languages use a
clear prefix for globals.
* you forget to declare a global
* or you declare a global too much or in conflict
* you have a local identical variable name and want to save/load it
to/from the global with same name
* while you add code, the definition of globals moves more and more apart
from their use cases -> weirdness; programmers thinking is fragmented
* using globals()[''xy''] is ''stringy non-program-code''
Thus, since long time at the head of my bigger modules I often put...
_global = sys.modules[__name__]
...and use global variables only/mainly like
def f(y):
v=x=_global.x
_global.y=y
...
for me this method is much more clear overall. And it is in line with the
clear exposition of "self" as regular object in Python methods (in a much
more self-similiar way compared to frozen @
m _... hacks相比,更加自我类似)>
我知道,这个家创建的_global创建了一个循环引用,但通常这个并不严重,因为模块只在程序结束时卸载。
(附带问题) :这是否可能会阻碍全局对象正确地运行;至少gc()在所有模块被关闭时运行
在退出())
不知何故我错过了一个很好的标准方法,可以在任何地方以未分段的方式使用全局变量。您怎么看?
Robert
m_... hacks in other
languages)
I know, this home created _global creates a circular ref, but usually this
is not serious as modules are unload only at the end of a program.
( side question: does this maybe hinder global objects from being
__del__''ed correctly; is at least gc() run when all modules are pulled off
at exit() )
Somehow I miss a nice standard method for using globals in an unfragmented
way everywhere. What do you think?
Robert
这篇关于global,globals(),_ global?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!