inspect

How to get all methods of a Python class with given decorator?

How to get all methods of a Python class with given decorator? Question: How to get all methods of a given class A that are decorated with the @decorator2? class A(): def method_a(self): pass @decorator1 def method_b(self, b): pass @decorator2 def method_c(self, t=5): pass Asked By: kraiz || Source Answers: Method 1: Basic registering decorator …

Total answers: 7

Inspect python class attributes

Inspect python class attributes Question: I need a way to inspect a class so I can safely identify which attributes are user-defined class attributes. The problem is that functions like dir(), inspect.getmembers() and friends return all class attributes including the pre-defined ones like: __class__, __doc__, __dict__, __hash__. This is of course understandable, and one could …

Total answers: 6

How to use inspect to get the caller's info from callee in Python?

How to use inspect to get the caller's info from callee in Python? Question: I need to get the caller info (what file/what line) from callee. I learned that I can use inpect module for that for purposes, but not exactly how. How to get those info with inspect? Or is there any other way …

Total answers: 4

How can I read a function's signature including default argument values?

How can I read a function's signature including default argument values? Question: Given a function object, how can I get its signature? For example, for: def my_method(first, second, third=’something’): pass I would like to get "my_method(first, second, third=’something’)". Asked By: Spì || Source Answers: Try calling help on an object to find out about it. …

Total answers: 9

How can I get a list of all classes within current module in Python?

How can I get a list of all classes within current module in Python? Question: I’ve seen plenty of examples of people extracting all of the classes from a module, usually something like: # foo.py class Foo: pass # test.py import inspect import foo for name, obj in inspect.getmembers(foo): if inspect.isclass(obj): print obj Awesome. But …

Total answers: 13

How to list all functions in a module?

How to list all functions in a module? Question: I have a Python module installed on my system and I’d like to be able to see what functions/classes/methods are available in it. I want to call the help function on each one. In Ruby I can do something like ClassName.methods to get a list of …

Total answers: 20