473,401 Members | 2,146 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,401 software developers and data experts.

list of all functions defined in the current module

Hi,

How can I get the list of all the functions defined in the current module?
Jul 18 '05 #1
3 19226
Fernando Rodriguez wrote:
Hi,

How can I get the list of all the functions defined in the current module?


Here's what I managed to do:

<listfunc.py>
import inspect

def listfunc():
me = __import__(inspect.getmodulename(__file__))
for name in dir(me):
obj = getattr(me, name)
if inspect.isfunction(obj):
yield obj

def foo(): pass

def bar(): pass
</lisfunc.py>

<d.py>
import listfunc

for func in listfunc.listfunc():
print func
</d.py>

It prints:
<function bar at 0x008ECAF0>
<function foo at 0x008ECAB0>
<function listfunc at 0x008F3430>

regards,
anton.

Jul 18 '05 #2
Fernando Rodriguez wrote:
Hi,

How can I get the list of all the functions defined in the current module?


If you have

def getfunctions(module):
import types
l = []
for key, value in module.__dict__.items():
if type(value) is FunctionType:
l.append(value)
return l

Then you can call this function with getfunctions(sys.modules[__name__])
to get a list of functions in the current module.

Perhaps you could also use the builtin 'inspect' module for this task.

-- Gerhard
Jul 18 '05 #3
In article <ma************************************@python.org >,
Gerhard Haring <gh@ghaering.de> wrote:
Fernando Rodriguez wrote:
Hi,

How can I get the list of all the functions defined in the current module?


If you have

def getfunctions(module):
import types
l = []
for key, value in module.__dict__.items():
if type(value) is FunctionType:
l.append(value)
return l

Then you can call this function with getfunctions(sys.modules[__name__])
to get a list of functions in the current module.

Perhaps you could also use the builtin 'inspect' module for this task.


If you just want a list of a module's globals, what's wrong with
dir(modulename)
?

Of course it will tell you about globals that are not functions (e.g.
classes) but maybe that's what the original poster wanted anyway.

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #4

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

Similar topics

5
by: Diez B. Roggisch | last post by:
Hi, this strikes me as a pretty basic question, but google didn't help, so I'm asking it here: How do I get a list of functions defined in a module in the module itself? Like this: ---...
1
by: John Hunter | last post by:
What is the best way to get a list of all functions actually defined in a module, ignoring those functions that the module may have imported but not defined? The following gets a list of all the...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
2
by: David Emme | last post by:
Access 97 I have a number of SELECT statements which contain references to user-defined VBA functions. These typically work as expected, but occasionally, on one user's machine or another,...
5
by: Little | last post by:
I have this program and I need to work on the test portion, which tests if a Val is in the list. It returns false no matter what could you look at the part and see what might need to be done to fix...
11
by: Josiah Manson | last post by:
In the following program I am trying to learn how to use functional programming aspects of python, but the following program will crash, claiming that the recursion depth is too great. I am...
4
Rabbit
by: Rabbit | last post by:
Cascading Combo/List Boxes This tutorial is to guide you in the creation of Cascading combo/list boxes. That is when you have multiple combo/list boxes where the selection of an option in one...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.