这是一个有效的导入序列吗? [英] is this a valid import sequence ?
问题描述
这可能是一个非常奇怪的构造,
但它是将另一种语言翻译成Python(用于模拟)的最简单方法。
虽然它有效,但我想知道这是否是一个有效的结构:
我已经定义了一个类,就像这样,
附加一个尚未全局定义
类T6963_device(tDevice):
def __init__(个体经营):
全球液晶显示器
LCD = self
在同一模块中我有一个函数,
运行上述类实例的方法,
并使用在类的init中定义的全局
def Write_LCD_Data(数据):
全局LCD
LCD .Write_Data(数据)
在另一个模块中,以正常方式创建一个且只有一个类的实例,
:
Graphical_LCD = T6963_device('''',Pos = [196,240],Color = wx.CYAN,Timer_On = True)
谢谢
Stef Mientki
2007年6月22日星期五21:43:40 +0200,Stef Mientki写道:
这可能是一个非常奇怪的结构,
但它是将另一种语言翻译成Python(用于模拟)的最简单方法。
虽然它有效但我想知道这是否是一个有效的结构:
既然它有效,怎么能不是一个有效的结构?
但是,使用全局变量几乎总是一个坏主意。传递
参数在Python中非常便宜,这几乎总是更好的
解决方案。
-
史蒂文。
谢谢史蒂文,
Steven D''Aprano写道:
星期五,2007年6月22日21:43:40 +0200,Stef Mientki写道:
>这可能是一个非常奇怪的构造,
但它是将另一种语言翻译成Python(用于模拟)的最简单方法。
虽然它有效但我想知道这是否有效建设:
既然它有效,它怎么可能不是一个有效的结构?
ok这似乎是合情合理的推理
>
但是,使用全局变量几乎总是一个坏主意。传递
参数在Python中真的很便宜,这几乎总是一个更好的
解决方案。
是的我知道,
但这是真实的东西的事实。我正在模拟;-)
欢呼,
Stef
>
Stef Mientki写道:
...我已经定义了一个类,比如这个,...
类T6963_device(tDevice):
def __init__(个体经营):
全球液晶显示器>
LCD = self
...在同一模块中我有一个函数,
运行上述类实例的方法,... 。
def Write_LCD_Data(数据):
全局LCD
LCD.Write_Data(数据)
Write_LCD_Data中的全局语句完全没必要。
只有你需要的时间才是全球的如果你想将全局
名称重新关联到另一个对象(例如LCD = LCD + 1或其他)。您只需要
读取全局名称到对象的映射(尽管您可能在命名对象上使用方法
来更改引用的对象)。你只需要
" global"当你需要写时(重新绑定)全局名称到对象
映射。
--Scott David Daniels
sc *********** @ acm.org
This might be a very weird construction,
but it''s the most easy way in translating another language into Python (for simulation).
Although it works, I like to know if this a valid construction:
I''ve defined a class, like this,
attaching a not yet defined global to itself
class T6963_device (tDevice):
def __init__ (self):
global LCD
LCD = self
In the same module I''ve a function,
that runs a method of the above class instance,
and uses the global defined in the init of the class
def Write_LCD_Data ( data ):
global LCD
LCD.Write_Data ( data )
In another module a create one and only one instance of the class,
in the normal way:
Graphical_LCD = T6963_device('''', Pos=[196,240], Color=wx.CYAN, Timer_On=True)
thanks
Stef Mientki
On Fri, 22 Jun 2007 21:43:40 +0200, Stef Mientki wrote:
This might be a very weird construction,
but it''s the most easy way in translating another language into Python (for simulation).
Although it works, I like to know if this a valid construction:Since it works, how can it NOT be a valid construction?
However, using global variables is almost always a bad idea. Passing
parameters around is really cheap in Python, that''s almost always a better
solution.
--
Steven.
thanks Steven,
Steven D''Aprano wrote:On Fri, 22 Jun 2007 21:43:40 +0200, Stef Mientki wrote:
>This might be a very weird construction,
but it''s the most easy way in translating another language into Python (for simulation).
Although it works, I like to know if this a valid construction:
Since it works, how can it NOT be a valid construction?ok that seems a plausible reasoning
>
However, using global variables is almost always a bad idea. Passing
parameters around is really cheap in Python, that''s almost always a better
solution.Yes I know,
but that''s a fact of the "real thing" I''m simulating ;-)
cheers,
Stef>
Stef Mientki wrote:... I''ve defined a class, like this, ...
class T6963_device (tDevice):
def __init__ (self):
global LCD
LCD = self
... In the same module I''ve a function,
that runs a method of the above class instance, ...
def Write_LCD_Data ( data ):
global LCD
LCD.Write_Data ( data )The global statement in Write_LCD_Data is completely unnecessary. The
only time you need "global" is if you want to reassociate the global
name to another object (such as LCD = LCD + 1 or whatever). You only
read the global name-to-object mapping (though you may be using methods
on the named object to alter the referenced object). You only need
"global" when you need to "write" (re-bind) the global name-to-object
mapping.
--Scott David Daniels
sc***********@acm.org
这篇关于这是一个有效的导入序列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!