英文:
Receiving the instance of a already called module in another file without calling it again
问题
我有一个关于变量和将它们传递给类方法的问题。我有一个名为OM的脚本,其中包含了OM类。
class OM:
def __init__(Self, Debug_Mode=False):
print("Initialized")
Self.Debug_Mode = Debug_Mode
Self.Registered_Modules = {}
我还有这个配置脚本:
class Config:
def __init__(Self):
OM = __import__('OM')
Self.OM = OM.OM.Load_Module()
print(Self.OM)
Self.Config_Entries = {}
由于脚本OM已经被调用过,我希望在不再调用它的情况下传递实例对象。因此,我在我的__init__函数中打印了'Initialized'。
当我执行我的文件时,“Initialized”字符串将被打印两次,因为它在我的起始脚本中已经被初始化:
class VTE:
def __init__(Self):
Self.OM = OM(Debug_Mode=False)
CFG = Self.OM.Load_Module('Config')
CFG.Config_Loader()
为了导入实例(像<OM.OM object at x000002047D4ACC10>)而不再次调用它,我需要在这里做什么更改:
OM = __import__('OM')
Self.OM = OM.OM()
print(Self.OM)
Self.Config_Entries = {}
非常感谢和最好的问候。
---------------------------------------------------------------------------
编辑:
我现在想出了如何做到这一点。模块OM的实例在Self中,
所以我在__init__()之外定义了一个名为OM_Handler的变量。在__init__()内部,我将Self分配给OM_Handler:
class OM :
OM_Handler = "None"
def __init__(Self, Debug_Mode=False) :
OM.OM_Handler = Self
Self.Debug_Mode = Debug_Mode
Self.Registered_Modules = {}
然后,在我的配置文件中,我能够调用实例对象:
class Config() :
def __init__(Self) :
Self.Config_Entries = {}
OM = __import__('OM')
OM_Handler = OM.OM.OM_Handler
Self.OM = OM_Handler
print(Self.OM)
打印出<OM.OM object at 0x0000026F5812DFA0>
但我不太确定这是否是最佳解决方案。
我被告知,全局变量是严格禁止的,或者有不良声誉。我希望你能给我一些建议。
最好的问候
NumeroUnoDE
英文:
I have a question concerning variables and passing them to class methods. I have a script OM with the class OM in it.
class OM:
def __init__( Self , Debug_Mode = False ):
print( "Initialized" )
Self.Debug_Mode = Debug_Mode
Self.Registered_Modules = { }
I also have this config script :
class Config:
def __init__( Self ) :
OM = __import__( 'OM' )
Self.OM = OM.OM.Load_Module( )
print( Self.OM )
Self.Config_Entries = { }
As the script OM has been already called, I want to hand over the instance object WITHOUT calling it. For this reason, I printed 'Initialized' in my __init__
function.
When I execute my file, the "Initialized' string will be printed 2 times because it has been initialized before by my start script :
class VTE:
def __init__( Self ) :
Self.OM = OM( Debug_Mode = False )
CFG = Self.OM.Load_Module( 'Config' )
CFG.Config_Loader( )
What do I have to change here for importing the instance ( like <OM.OM object at x000002047D4ACC10>
) without calling it again :
OM = __import__( 'OM' )
Self.OM = OM.OM( )
print( Self.OM )
Self.Config_Entries = { }
Thanks a lot and best regards.
EDIT :
I now figured out how to do it. The instance of the module OM
is in Self
,
so i defined a variable OM_Handler
outside of __init__( )
. Inside Of __init__
, i assigned Self
to OM_Handler
:
class OM :
OM_Handler = "None"
def __init__( Self , Debug_Mode = False ) :
OM.OM_Handler = Self
Self.Debug_Mode = Debug_Mode
Self.Registered_Modules = { }
Afterwards, i'm able to call the instance object in my config file :
class Config( ) :
def __init__( Self ) :
Self.Config_Entries = { }
OM = __import__( 'OM' )
OM_Handler = OM.OM.OM_Handler
Self.OM = OM_Handler
print( Self.OM )
Prints <OM.OM object at 0x0000026F5812DFA0>
But i'm not quite sure, if this is the best solution.
I have been told, that global variables are strictly
forbidden or rather have a bad repute.I hope, you will
give me some advices.
Best regards
NumeroUnoDE
答案1
得分: 0
你在这里混淆了“模块”和“类”的概念。当你有一个同名的模块和类时,这个区别尤为重要。如果你的意图是要创建一个单例的OM对象(在整个应用程序中全局唯一),你可以按照以下方式修改OM.py文件。
class OM:
def __init__(self, Debug_Mode=False):
print("初始化")
self.Debug_Mode = Debug_Mode
self.Registered_Modules = {}
OM = OM()
现在,在你的主要代码中,或者在任何需要它的模块中,你可以这样写:
...
import OM
class Config():
def __init__(self):
self.OM = OM.OM
或者,更简单的方式:
from OM import OM
class Config():
def __init__(self):
self.OM = OM
将变量名大写不是一个良好的实践,特别是将self
大写是非常糟糕的。在Python中,这种拼写是国际标准。虽然它不是严格的保留字,但它几乎可以看作是一个。
英文:
You are confusing the concepts of "module" and "class" here. That distinction is especially important when you have a module and a class with the same name. If your INTENTION is to have a singleton OM object (one global for the whole app), you can do that by having OM.py look like this.
class OM:
def __init__( self , Debug_Mode = False ):
print( "Initialized" )
self.Debug_Mode = Debug_Mode
self.Registered_Modules = { }
OM = OM()
Now, in your main code, or in any module where you need it, you can write:
...
import OM
class Config():
def __init__( self ):
self.OM = OM.OM
Or, even simpler:
from OM import OM
class Config():
def __init__( self ):
self.OM = OM
It's not good practice to capitalize variable names, and it is ESPECIALLY bad to capitalize the name self
. That spelling is an international standard in Python. It's not technically a reserved word, but it might as well be.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论