473,378 Members | 1,067 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,378 software developers and data experts.

module functions list

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:

--- module functions.py
def foo():
return 1

def bar():
return 1
def all_functions():
return <some magic code that yields [foo, bar]>

---

The application for this is that I want a bunch of predefined functions for
a small expression interpreter of mine to be collected in one module and
get them all without an explicit "registration" process.

Any ideas?

--
Regards,

Diez B. Roggisch
Jul 18 '05 #1
5 2649
Hi,

You could use a list comprehension coupled with a filter:

functionList = [function for function in dir(objectName) \
if callable(getattr(objectName,function))]

mark

On Wed, 25 Aug 2004 13:45:11 +0200, Diez B. Roggisch <de*********@web.de> wrote:
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:

--- module functions.py
def foo():
return 1

def bar():
return 1

def all_functions():
return <some magic code that yields [foo, bar]>

---

The application for this is that I want a bunch of predefined functions for
a small expression interpreter of mine to be collected in one module and
get them all without an explicit "registration" process.

Any ideas?

--
Regards,

Diez B. Roggisch
--
http://mail.python.org/mailman/listinfo/python-list

Jul 18 '05 #2
Hi,
You could use a list comprehension coupled with a filter:

functionList = [function for function in dir(objectName) \
if callable(getattr(objectName,function))]


That works if objectName is my module - but how do I get a reference to the
current module _inside_ my module? Thats the hart part (at least for
me ...)

--
Regards,

Diez B. Roggisch
Jul 18 '05 #3

dir(MODULE)

print type(dir(MODULE)) #===>>> <type 'list'>


Jul 18 '05 #4
Diez B. Roggisch wrote:
Hi,
You could use a list comprehension coupled with a filter:

functionList = [function for function in dir(objectName) \
if callable(getattr(objectName,function))]

Note that callable() will not filter out classes. If you want to exclude
classes from your list, have a look at inspect.isfunction() or
inspect.isroutine().
That works if objectName is my module - but how do I get a reference to
the current module _inside_ my module? Thats the hart part (at least for
me ...)


sys.modules[__name__]

Peter
Jul 18 '05 #5
> sys.modules[__name__]

Ahh, that did it. Thanks. So far I don't care for classes vs. functions, but
the introspect tip will eventually come to use one day...

--
Regards,

Diez B. Roggisch
Jul 18 '05 #6

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

Similar topics

3
by: Fernando Rodriguez | last post by:
Hi, How can I get the list of all the functions defined in the current module?
1
by: Peter Ĺstrand | last post by:
There's a new PEP available: PEP 324: popen5 - New POSIX process module A copy is included below. Comments are appreciated. ---- PEP: 324 Title: popen5 - New POSIX process module
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 =...
0
by: Stefan Behnel | last post by:
Hi! I filed a patch for a Heap class to be integrated into the heapq module (in addition the the currently available functions). The main features are support for iteration and for the standard...
17
by: Jacob Page | last post by:
I have created what I think may be a useful Python module, but I'd like to share it with the Python community to get feedback, i.e. if it's Pythonic. If it's considered useful by Pythonistas, I'll...
8
by: Floris van Haaster | last post by:
Hi All! I have a question, i have a web application and I store some member information in a variable i declared in a module like: Public some_info_variable as string in module1.vb But...
0
by: Steven Bethard | last post by:
Ok, so I have a module that is basically a Python wrapper around a big lookup table stored in a text file. The module needs to provide a few functions:: get_stem(word, pos, default=None)...
10
by: Tom Plunket | last post by:
I've got a bunch of code that runs under a bunch of unit tests. It'd be really handy if when testing I could supply replacement functionality to verify that the right things get called without...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...

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.