[Tutor] reference to main module

Alan Gauld alan.gauld at blueyonder.co.uk
Fri Dec 12 03:49:13 EST 2003


Luiz,

I'm adding the tutor list to the reply because someone 
there might have a better idea.

My response is below...

----- Original Message ----- 
From: "Luiz Siqueira" <cybersamurai at terra.com.br>
To: "Alan Gauld" <alan.gauld at blueyonder.co.uk>
Sent: Friday, December 12, 2003 12:17 AM
Subject: Re: [Tutor] reference to main module


> Sorry, I am not very good with words.  : )
> 
> I make a mistake,
> please change getattrib to getattr.
> 
> My idea is use a name of some data in XML
> to call the correct function in current module
> without needed of make a declaration for all
> function. In this way I can grow the number of
> functions easily.

To use a function you must declare it somewhere, 
or more accurately you must define it somewhere.

Now what I think you mean here is that you want 
to read the name of a function from an XML file
(the creator of which would need to know the 
function name in advance?) and execute it?

The normal way to do this is to use a dictionary.
You can either create your own, or use the module's 
internal dictionary. Personally I prefer to 
create my own and leave Python to mess with its 
own internals.

Something like:

def foo(): pass
def bar(): pass

my_funcs = {
"foo" : foo,
"bar" : bar
}

Then I can process the file(pseudo code warning!):

for line in xmlmsg:
    if line in my_funcs:
       my_funcs[line]()


Is that what you mean?
And of course you could map any name to the functions, 
the dictionary key does not need to match the function name.

> suppose I have a data type Carnaval.
> Than I use the type name to call the
> correct function, and if inside of this
> data I have another like Samba, simple I
> repeat the same cycle.
> 
> ---
> Tell me if you now can understand. :)

I think so, but if what I described above isn't what you 
mean, then sorry, no.

Alan G.



More information about the Tutor mailing list