在另一个文件中接收已调用的模块的实例,而无需再次调用它。

huangapple go评论112阅读模式
英文:

Receiving the instance of a already called module in another file without calling it again

问题

  1. 我有一个关于变量和将它们传递给类方法的问题我有一个名为OM的脚本其中包含了OM
  2. class OM:
  3. def __init__(Self, Debug_Mode=False):
  4. print("Initialized")
  5. Self.Debug_Mode = Debug_Mode
  6. Self.Registered_Modules = {}
  7. 我还有这个配置脚本
  8. class Config:
  9. def __init__(Self):
  10. OM = __import__('OM')
  11. Self.OM = OM.OM.Load_Module()
  12. print(Self.OM)
  13. Self.Config_Entries = {}
  14. 由于脚本OM已经被调用过我希望在不再调用它的情况下传递实例对象因此我在我的__init__函数中打印了'Initialized'
  15. 当我执行我的文件时,“Initialized字符串将被打印两次因为它在我的起始脚本中已经被初始化
  16. class VTE:
  17. def __init__(Self):
  18. Self.OM = OM(Debug_Mode=False)
  19. CFG = Self.OM.Load_Module('Config')
  20. CFG.Config_Loader()
  21. 为了导入实例<OM.OM object at x000002047D4ACC10>而不再次调用它我需要在这里做什么更改
  22. OM = __import__('OM')
  23. Self.OM = OM.OM()
  24. print(Self.OM)
  25. Self.Config_Entries = {}
  26. 非常感谢和最好的问候
  27. ---------------------------------------------------------------------------
  28. 编辑
  29. 我现在想出了如何做到这一点模块OM的实例在Self
  30. 所以我在__init__()之外定义了一个名为OM_Handler的变量__init__()内部我将Self分配给OM_Handler
  31. class OM :
  32. OM_Handler = "None"
  33. def __init__(Self, Debug_Mode=False) :
  34. OM.OM_Handler = Self
  35. Self.Debug_Mode = Debug_Mode
  36. Self.Registered_Modules = {}
  37. 然后在我的配置文件中我能够调用实例对象
  38. class Config() :
  39. def __init__(Self) :
  40. Self.Config_Entries = {}
  41. OM = __import__('OM')
  42. OM_Handler = OM.OM.OM_Handler
  43. Self.OM = OM_Handler
  44. print(Self.OM)
  45. 打印出<OM.OM object at 0x0000026F5812DFA0>
  46. 但我不太确定这是否是最佳解决方案
  47. 我被告知全局变量是严格禁止的或者有不良声誉我希望你能给我一些建议
  48. 最好的问候
  49. NumeroUnoDE
英文:

I have a question concerning variables and passing them to class methods. I have a script OM with the class OM in it.

  1. class OM:
  2. def __init__( Self , Debug_Mode = False ):
  3. print( &quot;Initialized&quot; )
  4. Self.Debug_Mode = Debug_Mode
  5. Self.Registered_Modules = { }

I also have this config script :

  1. class Config:
  2. def __init__( Self ) :
  3. OM = __import__( &#39;OM&#39; )
  4. Self.OM = OM.OM.Load_Module( )
  5. print( Self.OM )
  6. 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 :

  1. class VTE:
  2. def __init__( Self ) :
  3. Self.OM = OM( Debug_Mode = False )
  4. CFG = Self.OM.Load_Module( &#39;Config&#39; )
  5. CFG.Config_Loader( )

What do I have to change here for importing the instance ( like &lt;OM.OM object at x000002047D4ACC10&gt; ) without calling it again :

  1. OM = __import__( &#39;OM&#39; )
  2. Self.OM = OM.OM( )
  3. print( Self.OM )
  4. 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 :

  1. class OM :
  2. OM_Handler = &quot;None&quot;
  3. def __init__( Self , Debug_Mode = False ) :
  4. OM.OM_Handler = Self
  5. Self.Debug_Mode = Debug_Mode
  6. Self.Registered_Modules = { }

Afterwards, i'm able to call the instance object in my config file :

  1. class Config( ) :
  2. def __init__( Self ) :
  3. Self.Config_Entries = { }
  4. OM = __import__( &#39;OM&#39; )
  5. OM_Handler = OM.OM.OM_Handler
  6. Self.OM = OM_Handler
  7. print( Self.OM )

Prints &lt;OM.OM object at 0x0000026F5812DFA0&gt;

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文件。

  1. class OM:
  2. def __init__(self, Debug_Mode=False):
  3. print("初始化")
  4. self.Debug_Mode = Debug_Mode
  5. self.Registered_Modules = {}
  6. OM = OM()

现在,在你的主要代码中,或者在任何需要它的模块中,你可以这样写:

  1. ...
  2. import OM
  3. class Config():
  4. def __init__(self):
  5. self.OM = OM.OM

或者,更简单的方式:

  1. from OM import OM
  2. class Config():
  3. def __init__(self):
  4. 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.

  1. class OM:
  2. def __init__( self , Debug_Mode = False ):
  3. print( &quot;Initialized&quot; )
  4. self.Debug_Mode = Debug_Mode
  5. self.Registered_Modules = { }
  6. OM = OM()

Now, in your main code, or in any module where you need it, you can write:

  1. ...
  2. import OM
  3. class Config():
  4. def __init__( self ):
  5. self.OM = OM.OM

Or, even simpler:

  1. from OM import OM
  2. class Config():
  3. def __init__( self ):
  4. 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.

huangapple
  • 本文由 发表于 2023年7月6日 14:15:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625993.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定