File Paramaterized Abstract Factory

Charles Krug cdkrug at aol.com
Wed Jan 11 08:11:30 EST 2006


On 2006-01-11, Fredrik Lundh <fredrik at pythonware.com> wrote:
> Charles Krug wrote:
>
>> What I'd like is to do something like this:
>>
>> factoryFile = sys.argv[1] # we assume that argv[1] implements a
>>                           # correct ThingMaker interface.
>
> sys.argv[1] is a string, so I assume that you meant to say that
> the module named by argv[1] implements the correct interface.

Yes.

>
>> Anywho, my problem is with the whole Miracle thing.
>>
>> I've tried using __import__:
>>
>> a = 'HerFactory'
>> __import(a)
>>
>> Which returns:
>>
>> <module 'HerFactory' from 'HerFactory.py'>
>
> try:
>
>     factoryObject = __import__(factoryFile)
>     print dir(factoryObject)
>

Ah Ha!

That's what comes of working from memory instead of letting the computer
remember for me.

>>> factoryFile = 'ThingMaker'
>>> factoryObject = __import__(factoryFile)
>>> print dir(factoryObject)
['MakeOtherThing', 'MakeThing', '__builtins__', '__doc__', '__file__', 
'__name__']
>>> factoryObject.MakeThing()
I made a Thing!

Wonderful!  Exactly what I'm after, thanks.

> this should give you a list of the functions in the given module,
> or an ImportError if it doesn't exist.
>

Yes, that's what I was getting.  Should have pasted that as well, but I
was feeling grumpy and impatient.

Thanx


Charles



More information about the Python-list mailing list