Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Does python have an internal data structure with functions importedfrom a module?

Reply
Thread Tools

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

 
 
Carl J. Van Arsdall
Guest
Posts: n/a
 
      02-17-2006
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
Build and Release
MontaVista Software

 
Reply With Quote
 
 
 
 
Georg Brandl
Guest
Posts: n/a
 
      02-17-2006
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
 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      02-17-2006
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
 
Reply With Quote
 
Larry Bates
Guest
Posts: n/a
 
      02-17-2006
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

 
Reply With Quote
 
=?ISO-8859-1?Q?Sch=FCle_Daniel?=
Guest
Posts: n/a
 
      02-18-2006
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+y,
"minus" : lambda x,y-y,
"power" : lambda x,y**y,
}

op["power"](2,

hth, Daniel

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple structure and copying data to pointer of the same structure A C++ 27 04-16-2011 11:07 PM
Does ruby have a similar functions to the Python dir and help functions? py Ruby 5 03-24-2007 08:54 AM
Does python have an internal list/dictionary of functions? Carl J. Van Arsdall Python 1 02-16-2006 05:51 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
External inline functions calling internal inline functions Daniel Vallstrom C Programming 2 11-21-2003 01:57 PM



Advertisments