[Tutor] Dynamic Function Calls

Megan Land mland at us.ibm.com
Fri Aug 14 22:30:14 CEST 2009


Dave Angel <davea at ieee.org> wrote on 08/14/2009 03:53:30 PM:

> From:
>
> Dave Angel <davea at ieee.org>
>
> To:
>
> Megan Land/Raleigh/Contr/IBM at IBMUS
>
> Cc:
>
> bob gailer <bgailer at gmail.com>, tutor at python.org
>
> Date:
>
> 08/14/2009 03:53 PM
>
> Subject:
>
> Re: [Tutor] Dynamic Function Calls
>
> Megan Land wrote:
> >

> >   From:       Dave Angel <davea at ieee.org>

> >

> >   To:         bob gailer <bgailer at gmail.com>

> >

> >   Cc:         Megan Land/Raleigh/Contr/IBM at IBMUS, tutor at python.org

> >

> >   Date:       08/14/2009 01:53 PM

> >

> >   Subject:    Re: Re: [Tutor] Dynamic Function Calls

> >

> >
> >
> >
> >
> >
> >
> >
> >
> > bob gailer wrote:
> >
> >> <div class="moz-text-flowed" style="font-family: -moz-fixed">Megan
> >> Land wrote:
> >>
> >>> All three methods are defined below the snippet I provided.
> >>>
> >>>
> >> In Python names must be defined before they are referenced. Put these
> >> defs above the snippet.
> >>
> >>
> >>> def func():
> >>> code...
> >>> def func0():
> >>> do stuff
> >>> def func1():
> >>> do stuff
> >>> def func2():
> >>> do stuff
> >>>
> >>> Megan Land
> >>> FVT Blade EMET Test Engineer
> >>> mland at us.ibm.com
> >>>
> >>> Inactive hide details for Kent Johnson ---08/13/2009 05:18:10 PM---On
> >>> Thu, Aug 13, 2009 at 3:30 PM, Megan Land<mland at us.ibm.comKent Johnson
> >>> ---08/13/2009 05:18:10 PM---On Thu, Aug 13, 2009 at 3:30 PM, Megan
> >>> Land<mland at us.ibm.com> wrote: > Hi,
> >>>
> >>>
> >>> From:
> >>> Kent Johnson <kent37 at tds.net>
> >>>
> >>> To:
> >>> Megan Land/Raleigh/Contr/IBM at IBMUS
> >>>
> >>> Cc:
> >>> tutor at python.org
> >>>
> >>> Date:
> >>> 08/13/2009 05:18 PM
> >>>
> >>> Subject:
> >>> Re: [Tutor] Dynamic Function Calls
> >>>
> >>> Sent by:
> >>> kent3737 at gmail.com
> >>>
> >>>
------------------------------------------------------------------------
> >>>
> >>>
> >>>
> >>> On Thu, Aug 13, 2009 at 3:30 PM, Megan Land<mland at us.ibm.com> wrote:
> >>>
> >>>> Hi,
> >>>>
> >>>> I'm trying to call a function from a dictionary. I did some
> >>>>
> >>> googling and
> >>>
> >>>> from what I can tell my code should work, but doesn't. Here's an
> >>>>
> >>> example:
> >>>
> >>>> def myFunc(self, inputList):
> >>>> dict={0: func0, 1: func1, 2:func2}
> >>>> for element in inputList:
> >>>> dict[element]()
> >>>>
> >>>> When I go to run this I get an error saying func0 is not defined.
Does
> >>>> anyone have any ideas as to why this won't work? I'm using Python
> >>>>
> >>> 2.6 if
> >>>
> >>>> that makes any difference.
> >>>>
> >>> You don't show any definition for func0 in the above snippet. Where
is
> >>> it defined?
> >>>
> >>> Kent
> >>>
> >>>
------------------------------------------------------------------------
> >>>
> >>> _______________________________________________
> >>> Tutor maillist  -  Tutor at python.org
> >>> http://mail.python.org/mailman/listinfo/tutor
> >>>
> >>>
> > You can put these defs in any order.  But when you invoke the function
> > from your outerlevel code, all of them need to have been defined.  I'm
> > guessing you had these in this order:
> >
> > <code>
> > def myFunc(self, inputList):
> >     dictionary={0: func0, 1: func1, 2:func2}
> >     for element in inputList:
> >         dictionary[element]()    ...
> >
> > myFunc(3, 1, 2, 1)                 #this is too early in the file,
> > because the following defs have not been defined yet
> >
> > def func():
> >      code...
> > def func0():
> >     do stuff
> > def func1():
> >     do stuff
> > def func2():
> >     do stuff
> >
> > #move the call to myFunc() here
> > </code>
> >
> >
> > Move the outerlevel code to the end, and you're usually better off.
You
> > also might want to put it inside an
> > if __name__ == "__main__":
> >
> > clause.
> >
> >
> > Note, I also changed the variable 'dict'  to 'dictionary,'  since dict
> > already has a meaning in Python.  Not a big deal in this particular
> > case, but if you ever wanted to convert a list to a dict, and called
> > dict(), you'd wonder what went wrong.  Better to kill the habit early.
> >
> > DaveA
> >
> >
> > I have the method inside a main method at the end of my program.  The
weird
> > thing is that I typed up my small example and ran it and it worked
fine.
> > But when I run my big program, I still get the error that func0, as I
call
> > it in my example, is not defined.  Do you  think there could be
something
> > else in my program that is making this go wrong?
> >
> >
> > Megan Land
> > FVT Blade EMET Test Engineer
> > mland at us.ibm.com
> >
> Something's wrong with your email program's quoting logic.  So your last
> response looks like it's part of my last email.
>
> :::  Do you think there could be something else....
>
> Yes, certainly.  Question is what.   You use the word method a couple of
> times above, yet none of your code shows classes.  Was that a typo?
>
> First thing is you should stop paraphrasing and describing, and include
> actual code, actual full error messages, and so on.  If you had actually
> run a small sample, you would have found out then that it worked, and
> that your description doesn't match the problem you're having with the
> full code.  Once you have a real code example that fails, use copy/paste
> so we see it exactly as it is.
>
> Next, you should realize that an error like :
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> NameError: name 'xyzzy' is not defined
>
> means that the symbol 'xyzzy' cannot be located in the valid namespaces
> of the current context.  So we need to see the context.  If it's a
> function, there is a local namespace and a global one.  Despite the
> word, it's only global to this particular module.   If it's a class
> method, or a nested function, the rules are different.  And the whole
> problem has nothing to do with dynamic function calls, as the happens
> before you ever get to the call.
>
> In front of that "dictionary=" line, insert a real call to func0(), and
> see if it gives a similar error.   Then try a call to     print
> help(__name__).  That should display all the global symbols for your
module.
>
> If func0() doesn't work in MyFunc(), then see if it works in the
> function that calls it.  And see if it works in your outer-level code,
> just before you call main().
>
> DaveA
>

Sorry about the email.  Apparently Lotus Notes doesn't like to put replies
at the end of the message.

I tried what you said.  I can call the function by itself (not within the
dictionary).  I also printed the help(__name__).  All of my functions were
listed.  Here's a more representative example of my code (I doing this for
work so I don't want to give my entire program away):

import os
class Test():
    def func(self):
        funcDict={".txt":text, ".html":html}
        exList=os.listdir("/home/megan/test")
        for i in exList:
          (filePath, fileName)=os.path.split(i)
          (name, extension)=os.path.splitext(fileName)
          self.funcDict[extension]()

    def text(self):
        print "This is a text file"

    def html(self):
        print "This is an html file"

def main():
    newTest=Test()
    newTest.func()


if __name__=="__main__":
    main()


Traceback (most recent call last):
  File "test.py", line 23, in <module>
    main()
  File "test.py", line 19, in main
    newTest.func()
  File "test.py", line 4, in func
    funcDict={".txt":text, ".html":html}
NameError: global name 'text' is not defined

It worked fine until I put it inside of a class, but I'm not sure what that
would have to do with it.  I don't need to have use all that, but now that
everything is setup like that I want to keep it that way.  Any help you can
provide will be greatly appreciated.

Megan Land
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090814/b0f7415f/attachment.htm>


More information about the Tutor mailing list