Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore imported functions in a python module when using getmembers(module, isfunction) [duplicate]

Is there a way to ignore imported functions in a python module?

When using the following module module.py:

from inspect import getmembers, isfunction
import foo

def boo():
   foo()

def moo():
   pass


funcs = [mem[0] for mem in getmembers(module, isfunction)]

funcs equals : ['boo','moo', 'foo'] (including imported function 'foo')

I want funcs to include ['boo', 'moo'] only.

like image 741
rok Avatar asked Sep 06 '25 03:09

rok


1 Answers

You'll have to test for the __module__ attribute; it is a string naming the full module path:

funcs = [mem[0] for mem in getmembers(module, isfunction)
         if mem[1].__module__ == module.__name__]
like image 127
Martijn Pieters Avatar answered Sep 07 '25 20:09

Martijn Pieters