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

question with inspect module

Hi,

I would like to retrieve all the classes, methods and functions of a
module.
I've used the inspect module for this, but inside a given class
(subclass of some other one), I wanted to retrieve only the methods
I've written, not the inherited one. How can I do ?

Thanks.

Feb 20 '07 #1
3 1198
Hello,
I would like to retrieve all the classes, methods and functions of a
module.
I've used the inspect module for this, but inside a given class
(subclass of some other one), I wanted to retrieve only the methods
I've written, not the inherited one. How can I do ?
class A:
def a_method(self):
pass

def common_method(self):
pass

class B(A):
def common_method(self):
pass

def b_method(self):
pass

import inspect
from os.path import abspath

lines, start_line = inspect.getsourcelines(B)
end_line = start_line + len(lines)
filename = abspath(inspect.getsourcefile(B))

for name in dir(B):
method = getattr(B, name)
if not callable(method):
continue

lnum = method.func_code.co_firstlineno
fname = abspath(method.func_code.co_filename)

if (lnum >= start_line) and (lnum <= end_line) and (fname ==
filename):
print "%s is from B" % name

HTH,
--
Miki Tebeka <mi*********@gmail.com>
http://pythonwise.blogspot.com

Feb 20 '07 #2
Thanks Miki, that's exactly what I need :)

Feb 20 '07 #3
En Tue, 20 Feb 2007 16:04:33 -0300, Tool69 <ki****************@gmail.com>
escribió:
I would like to retrieve all the classes, methods and functions of a
module.
I've used the inspect module for this, but inside a given class
(subclass of some other one), I wanted to retrieve only the methods
I've written, not the inherited one. How can I do ?
A more direct approach:

[name for name,value in vars(your_class).items() if
inspect.isroutine(value)]

This gets you normal, static and class methods. inspect.isfunction would
return only normal methods, and inspect.ismethoddescriptor static and
class methods.

--
Gabriel Genellina

Feb 21 '07 #4

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

Similar topics

2
by: Fernando Perez | last post by:
Hi all, IPython has suffered quite a few problems with the inspect module in python 2.3. For these, unfortunately all I've been able to do is guard with overreaching except clauses, as I had...
11
by: It's me | last post by:
I discovered the hardway what inspect.isclass() is doing. Consider this no brainer code: ### import inspect class Abc: def Hello(self): return
1
by: Darran Edmundson | last post by:
I was playing around with the inspect module the other day trying to write a quick and dirty "smart" logger. By this I mean that writing a message to the global log would also yield some info...
4
by: Benjamin Rutt | last post by:
I'm trying to learn about introspection in Python. my ultimate goal is to be able to build a module "text database" of all modules that are in the sys.path, by discovering all candidate modules...
0
by: Ron Adam | last post by:
While playing around with the inspect module I found that the Blockfinder doesn't recognize single line function definitions. Adding the following two lines to it fixes it, but I'm not sure if it...
6
by: DarkBlue | last post by:
My application makes several connections to a remote database server via tcp/ip. Usually all is fine,but occasionally the server is down or the internet does not work and then there is the 30 sec...
12
by: thomas.guest | last post by:
I'm not making progress with the following and would appreciate any help. Here's an interpreted Python session. .... Traceback (most recent call last): File "<stdin>", line 1, in <module>...
4
by: Chris Pax | last post by:
Hello, I recently been trying to use the inspect module to inspect the arguments of gtk objects, such as gtk.Button. I tried like this: inspect.getargspec(gtk.Button.__init__) and get the...
8
by: Aaron \Castironpi\ Brady | last post by:
Hello, The 'inspect' module has this method: inspect.getargvalues(frame) It takes a frame and returns the parameters used to call it, including the locals as defined in the frame, as shown....
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
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.