473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
cv*********@mvista.com
Build and Release
MontaVista Software

Feb 17 '06 #1
4 1188
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
Feb 17 '06 #2
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
Feb 17 '06 #3
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

Feb 17 '06 #4
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

Feb 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: David MacQuigg | last post by:
I am starting a new thread so we can avoid some of the non-productive argument following my earlier post "What is good about Prothon". At Mr. Hahn's request, I will avoid using the name "Prothon"...
8
by: Robert Oschler | last post by:
Has anybody seen a Python module that will take an XML document (not a colossal one), and convert it to a Python nested class object? I'm basically looking for something that would allow me to...
8
by: Bo Peng | last post by:
Dear list, I am writing a Python extension module that needs a way to expose pieces of a big C array to python. Currently, I am using NumPy like the following: PyObject* res =...
10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
11
by: diffuser78 | last post by:
I was a C Programmer for a while. Lately started to learn Python for one small project at school. I joined a small company where they use C++ for development. Can we use Python and C together ?...
162
by: Sh4wn | last post by:
Hi, first, python is one of my fav languages, and i'll definitely keep developing with it. But, there's 1 one thing what I -really- miss: data hiding. I know member vars are private when you...
13
by: John Dann | last post by:
A Python newbie, but some basic understanding of how classes, objects etc work in eg VB.Net. However, I'm struggling a little to translate this knowledge into the Python context. I'm trying to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.