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

get list of callable methods


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 callables, but includes functions
that some.module imports

funcs = []
for o in dir(some.module):
if o.startswith('_'): continue
p = getattr(some.module, o)
if not callable(p): continue
funcs.append(p)


JDH
Jul 18 '05 #1
1 2221
You could check to see if the module's source file matches the
function's source file:

# returns
# True if func was defined in mod's file,
# False if not _or_ if source file isn't known

def f_defined_by_module(func, mod):
try:
return func.func_code.co_filename == mod.__file__
except:
return False

# Return functions defined in mod
def module_defined_functions(mod):
return [f for f in mod.__dict__.itervalues() if callable(f) and \
f_defined_by_module(f, mod)]
But then again, there are plenty of ways to create modules or
functions that don't even have a source file...


John Hunter <jd******@ace.bsd.uchicago.edu> wrote in message news:<ma**************************************@pyt hon.org>...
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 callables, but includes functions
that some.module imports

funcs = []
for o in dir(some.module):
if o.startswith('_'): continue
p = getattr(some.module, o)
if not callable(p): continue
funcs.append(p)


JDH

Jul 18 '05 #2

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

Similar topics

21
by: Roy Smith | last post by:
I've got a function which gets passed a call-back function as a parameter. I want to check to make sure the thing passed in is indeed callable. Is: assert '__call__' in dir (param) the...
7
by: Duncan Smith | last post by:
I recently started to rewrite a class and decided to take a bunch of related methods and put them in a separate callable class. (They're all what I would call pointwise operations on pairs of...
28
by: The Eternal Squire | last post by:
PEP: 336 Title: Make None Callable Version: $Revision: 1.1 $ Last-Modified: $Date: 2004/11/03 16:58:30 $ Author: Andrew McClelland Status: Draft Type: Standards Track Content-Type: text/plain...
2
by: Mirko Zeibig | last post by:
Hello everybody, I recently stumbled across the proposal of removing `callable` in Python 3000 (http://python.org/peps/pep-3000.html) catching an exception instead, maybe something like this:...
17
by: Grant Edwards | last post by:
I'm trying to figure out how to sort a list, and I've run into a problem that that I have tripped over constantly for years: where are the methods of basic types documented? The only thing I can...
2
by: Aaron | last post by:
I have a data sructure setup and I populate it in a loop like so: y=0 while X: DS.name = "ASDF" DS.ID = 1234 list = DS; y = y + 1
10
by: Nicolas Fleury | last post by:
Hi everyone, I was wondering if it would make sense to make staticmethod objects callable, so that the following code would work: class A: @staticmethod def foo(): pass bar = foo() I...
6
by: Ron Garret | last post by:
If I do this: def f(self): print self class c1: pass setattr(c1, 'm1', f) Then f is automagically transmogrified into the appropriate sort of method depending on how it is used:
5
by: maduma | last post by:
Hi, Is the following code is ok. who to call all method. It is working but the call to m() without a reference to self seems strange Thanks for your help class CustomMethod: def...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.