#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    9
    Rep Power
    0

    Globals between moduls?


    The functionality of my main program is like this:
    import com

    global dataLock
    dataLock = threading.RLock()
    com = com_base.comtype('com') #Create thread that handles communication
    com.start()

    Now, in my com module I want to use dataLock, how do I get access to it? It all worked when everything was in one module but now it says that dataLock is undefined (which i understand) but how do I import datalock to com??
    "from main import dataLock" does not work for me
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You seem to be on the right track anyway: using the from-module-import statement. If you want to import from __main__ then you need to change main to __main__ in your import statement.

    Still, importing something into a module from __main__ seems like a bad idea to me. Personally I would put the variables definition inside the com module and then import it into the __main__ program.

    Note: that you don't need to define your variable as being global since it is being defined in the global namespace i.e.

    Code:
    >>> global a
    >>> a = 1
    >>> b = 2
    >>> 
    >>> def h():
    	print a
    
    	
    >>> h()
    1
    >>> def i():
    	print b
    
    	
    >>> i()
    2
    >>>
    You also seem to be overwriting the com module with your own variable named com.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    9
    Rep Power
    0
    Originally Posted by netytan
    Personally I would put the variables definition inside the com module and then import it into the __main__ program.
    Of course, now it works fine. Thank you Mark!

IMN logo majestic logo threadwatch logo seochat tools logo