0

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

NumeroUnoDE
  • 37
  • 1
  • 8
  • Can please someone give me an advice on this ? – NumeroUnoDE Jul 19 '23 at 16:59
  • It's not that "global variables are strictly forbidden", it's that it is very easy to abuse them. If you have an object that is providing some useful service to multiple modules in your code, or holding state information used across the app, then it is just fine to store that in a global. – Tim Roberts Jul 19 '23 at 17:56

1 Answers1

0

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.

Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
  • Hey Tim. Thanks for your advices. But unfortunately that doesen't work, as i need the instance :( i have different files like here start.py & myapp.py. I want to initialize The OM Instance once and pass it to all other parts of the application. Maybe i can pass you the 3 small files per e - mail, so it's easier to understand my reasons. :) – NumeroUnoDE Jul 19 '23 at 18:30
  • I don't think you understand what I wrote. It does EXACTLY what you are saying. In ANY file, if you do `from OM import OM`, you will get the one object instance, shared amongst all. Put the files in a git repo and post the link, if you want. – Tim Roberts Jul 20 '23 at 01:04
  • Hmm. Then i understand why it doesen't work. I uploaded the 3 files, the ```start.py``` , the ```myapp.py``` and the object manager ```OM.py``` here : https://file.io/xodFNkXH3eNb As i have ```OM.OM_Handler = Self``` in the ```__init__``` of the object manager, i now used just ```Self.OM = OM.OM_Handler``` to get the instance in ```myapp.py```. – NumeroUnoDE Jul 20 '23 at 05:03
  • 1
    Because you didn't use my code. The last line is the most important line, and you don't have it. Note that you don't really need to save it in `self.OM` -- just us `OM.` when you need to use the object. If I change your myapp.py to do that, your code works fine. – Tim Roberts Jul 20 '23 at 06:59
  • Indeed, i forgot to initialize the OM at the end. :) Can i now pass variables to the OM too, like i did before with the ```Debug_Mode``` trigger ? I think, you understand that i want to debug different things, especially at the object manager and the possibilty to pass other variables :) With this code i have to change ```Loader = OM( Debug_Mode = False )``` to ```Loader = OM``` – NumeroUnoDE Jul 20 '23 at 10:58
  • 1
    It depends on who gets to decide. You can set `Debug_Mode` in `OM.__init__`, or any other module can do `OM.Debug_Mode = True` at any time. – Tim Roberts Jul 20 '23 at 16:24