Does python have an internal data structure with functions importedfrom a module?

Discussion in 'Python' started by Carl J. Van Arsdall, Feb 17, 2006.

  1. Alright, I attempted to post this question yesterday but I don't see it
    as showing up, so I apologize in advance if this is a double post.

    Python Gurus:

    Let me elaborate a bit more on this question. Basically, I want to know
    if there is some data structure in python that maps a string function
    name to an address of a function or something to that nature.

    If this is confusing, let me describe what I want to do and see if
    anyone has any ideas.

    basically we have:

    >>>def functA():

    .... pass

    >>> functA

    <function functA at 80db128>

    And what I'd like to do is:

    >>>__internalFuncDict__['functA']

    <function functA at 80db128>

    This is just for a little experimental project of mine, any help or
    pointers to the proper pages in the manual would be greatly
    appreciated. Basically, I know that I can create something like this if
    I have to but I was very much hoping that something already existed
    somewhere that I could get to via python or by writing a C extension.

    Thanks in advance!

    -carl

    --

    Carl J. Van Arsdall

    Build and Release
    MontaVista Software
    Carl J. Van Arsdall, Feb 17, 2006
    #1
    1. Advertising

  2. Carl J. Van Arsdall

    Georg Brandl Guest

    Carl J. Van Arsdall wrote:
    > Alright, I attempted to post this question yesterday but I don't see it
    > as showing up, so I apologize in advance if this is a double post.
    >
    > Python Gurus:
    >
    > Let me elaborate a bit more on this question. Basically, I want to know
    > if there is some data structure in python that maps a string function
    > name to an address of a function or something to that nature.
    >
    > If this is confusing, let me describe what I want to do and see if
    > anyone has any ideas.
    >
    > basically we have:
    >
    > >>>def functA():

    > ... pass
    >
    > >>> functA

    > <function functA at 80db128>
    >
    > And what I'd like to do is:
    >
    > >>>__internalFuncDict__['functA']

    > <function functA at 80db128>


    Read about globals(), dir() and module.__dict__.

    Georg
    Georg Brandl, Feb 17, 2006
    #2
    1. Advertising

  3. Carl J. Van Arsdall

    Larry Bates Guest

    Carl J. Van Arsdall wrote:
    > Alright, I attempted to post this question yesterday but I don't see it
    > as showing up, so I apologize in advance if this is a double post.
    >
    > Python Gurus:
    >
    > Let me elaborate a bit more on this question. Basically, I want to know
    > if there is some data structure in python that maps a string function
    > name to an address of a function or something to that nature.
    >
    > If this is confusing, let me describe what I want to do and see if
    > anyone has any ideas.
    >
    > basically we have:
    >
    >>>>def functA():

    > ... pass
    >
    >>>> functA

    > <function functA at 80db128>
    >
    > And what I'd like to do is:
    >
    >>>>__internalFuncDict__['functA']

    > <function functA at 80db128>
    >
    > This is just for a little experimental project of mine, any help or
    > pointers to the proper pages in the manual would be greatly
    > appreciated. Basically, I know that I can create something like this if
    > I have to but I was very much hoping that something already existed
    > somewhere that I could get to via python or by writing a C extension.
    >
    > Thanks in advance!
    >
    > -carl
    >

    I would do this as follows:

    Create dictionary with the function names as keys and the pointer to
    function definition as value:

    def printFoo():
    print "Foo"
    return

    def printFOO():
    print "FOO"
    return

    fdict={'printFoo': printFoo, 'printFOO': printFOO}
    functions=('printFoo', 'printFOO')
    for function in function:
    if fdict.has_key(function: fdict[function]()
    else:
    print "No function named=%s defined" % function

    -Larry Bates
    Larry Bates, Feb 17, 2006
    #3
  4. Carl J. Van Arsdall

    Larry Bates Guest

    Re: Does python have an internal data structure with functionsimported from a module?

    Carl J. Van Arsdall wrote:
    > Alright, I attempted to post this question yesterday but I don't see it
    > as showing up, so I apologize in advance if this is a double post.
    >
    > Python Gurus:
    >
    > Let me elaborate a bit more on this question. Basically, I want to know
    > if there is some data structure in python that maps a string function
    > name to an address of a function or something to that nature.
    >
    > If this is confusing, let me describe what I want to do and see if
    > anyone has any ideas.
    >
    > basically we have:
    >
    >>>>def functA():

    > ... pass
    >
    >>>> functA

    > <function functA at 80db128>
    >
    > And what I'd like to do is:
    >
    >>>>__internalFuncDict__['functA']

    > <function functA at 80db128>
    >
    > This is just for a little experimental project of mine, any help or
    > pointers to the proper pages in the manual would be greatly
    > appreciated. Basically, I know that I can create something like this if
    > I have to but I was very much hoping that something already existed
    > somewhere that I could get to via python or by writing a C extension.
    >
    > Thanks in advance!
    >
    > -carl
    >

    I would do this as follows:

    Create dictionary with the function names as keys and the pointer to
    function definition as value:

    def printFoo():
    print "Foo"
    return

    def printFOO():
    print "FOO"
    return

    fdict={'printFoo': printFoo, 'printFOO': printFOO}
    functions=('printFoo', 'printFOO')
    for function in function:
    if fdict.has_key(function: fdict[function]()
    else:
    print "No function named=%s defined" % function

    -Larry Bates
    Larry Bates, Feb 17, 2006
    #4
  5. in case you are trying it in the python shell

    >>> def foo():return "test"

    ....
    >>> import __main__
    >>> __main__.__dict__["foo"]

    <function foo at 0x40420c6c>
    >>> __main__.__dict__["foo"]()

    'test'
    >>>


    otherwise build your own dict with string->function mapping

    op = {
    "plus" : lambda x,y:x+y,
    "minus" : lambda x,y:x-y,
    "power" : lambda x,y:x**y,
    }

    op["power"](2,8)

    hth, Daniel
    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Feb 18, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Daniel Vallstrom
    Replies:
    2
    Views:
    1,830
    Kevin Bracey
    Nov 21, 2003
  2. Replies:
    7
    Views:
    278
    Kent Johnson
    Dec 16, 2005
  3. Carl J. Van Arsdall
    Replies:
    1
    Views:
    335
    Raymond Hettinger
    Feb 16, 2006
  4. A
    Replies:
    27
    Views:
    1,578
    Jorgen Grahn
    Apr 17, 2011
  5. py
    Replies:
    5
    Views:
    288
    Daniel DeLorme
    Mar 24, 2007
Loading...

Share This Page