Adventures in Python, Part 1:

Attempt 1


MyPackage/init.py:
        MyGlobal = initialize()
client.py:
        from MyPackage import MyGlobal  # FAIL; MyGlobal isn't defined.

Attempt 2


MyPackage/init.py:
        MyGlobal = initialize()
        def getter():
            return MyGlobal
client.py:
        from MyPackage import getter
        MyGlobal = getter()
MyPackage/otherthing.py:
        MyGlobal.doSomething()  # FAIL: MyGlobal isn't global to the package

Attempt 3


MyPackage/globals.py:
        MyGlobal = None
MyPackage/init.py: from globals import MyGlobal MyGlobal = initialize() def getter(): return MyGlobal MyPackage/otherthing.py: from globals import MyGlobal
MyGlobal.doSomething() # FAIL; MyGlobal == None

Attempt 4


MyPackage/globals.py:
        MyGlobal = None
MyPackage/init.py: import globals globals.MyGlobal = initialize() def getter(): return MyGlobal MyPackage/otherthing.py: from globals import MyGlobal
MyGlobal.doSomething() # Success, finally

Oh well. It still beats EnterpriseVisualBeans.


#   Posted 2013-03-06 20:16:00 UTC; last changed 2013-03-06 20:27:00 UTC