On 6/24/2011 7:30 AM, Gnarlodious wrote:
On Jun 24, 12:27 am, Terry Reedy wrote:

1) Can I tell Executable.py to share Data with ModuleTest.py?

After the import is complete, yes.
import ModuleTest
ModuleTest.Data = Data

This works if the use of Data is inside a function that is not called
during import, not if the use of Data is at toplevel or in a class
statement outside a def.

That works! The solution looks like this:

# controlling program:
from Module import Data
import ModuleTest
ModuleTest.Data = Data
ModuleTest.getData()

# module:
def getData():
     print(Data.Plist.Structure)

This is a form of dependency injection, where a caller injects into a callee a dependency (callee) of the callee. It can be used even if the callee imports a dependency when it is imported. It is useful for testing when you want the callee to use a different dependency for testing. Simple example:

# MyModule
import socket
def myconnect(*args):
   ... socket.bind()

# Test_MyModule
import mock_socket
import MyModule
MyModule.socket = mock_socket
... test of MyModule, including MyModule.myconnect

Python makes this trivial without requiring that the otherwise constant depedency always be injected or passed (in normal production use) as a variable.

In your case, you are free to inject different forms of Data if you wish.

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to