introspection

How do you get all classes defined in a module but not imported?

How do you get all classes defined in a module but not imported? Question: I’ve already seen the following question but it doesn’t quite get me where I want: How can I get a list of all classes within current module in Python? In particular, I do not want classes that are imported, e.g. if …

Total answers: 5

Determine function name from within that function (without using traceback)

Determine function name from within that function (without using traceback) Question: In Python, without using the traceback module, is there a way to determine a function’s name from within that function? Say I have a module foo with a function bar. When executing foo.bar(), is there a way for bar to know bar‘s name? Or …

Total answers: 27

How is introspection useful?

How is introspection useful? Question: I have been programming mainly in PHP, and I am trying to make a switch to python. I am skilled with PHP, and I have never needed to use introspection / introspection like capabilities. What good is code introspection, and in what situations would I find it indispensable? Here is …

Total answers: 4

How to get current function into a variable?

How to get current function into a variable? Question: How can I get a variable that contains the currently executing function in Python? I don’t want the function’s name. I know I can use inspect.stack to get the current function name. I want the actual callable object. Can this be done without using inspect.stack to …

Total answers: 10

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

Introspection to get decorator names on a method?

Introspection to get decorator names on a method? Question: I am trying to figure out how to get the names of all decorators on a method. I can already get the method name and docstring, but cannot figure out how to get a list of decorators. Asked By: Tony || Source Answers: That’s not possible …

Total answers: 8

How to get the caller's method name in the called method?

How to get the caller's method name in the called method? Question: Python: How to get the caller’s method name in the called method? Assume I have 2 methods: def method1(self): … a = A.method2() def method2(self): … If I don’t want to do any change for method1, how to get the name of the …

Total answers: 12

How to introspect django model fields?

How to introspect django model fields? Question: I am trying to obtain class information on a field inside a model, when I only know name of the field and name of the model (both plain strings). How is it possible? I can load the model dynamically: from django.db import models model = models.get_model(‘myapp’,’mymodel’) Now I …

Total answers: 4

Python: How to retrieve class information from a 'frame' object?

Python: How to retrieve class information from a 'frame' object? Question: Is it possible to retrieve any class information from a frame object? I know how to get the file (frame.f_code.co_filename), function (frame.f_code.co_name) and line number (frame.f_lineno), but would like to be able to also get the name of the class of the active object …

Total answers: 6

How to check if an object is an instance of a namedtuple?

How to check if an object is an instance of a namedtuple? Question: How do I check if an object is an instance of a Named tuple? Asked By: Sridhar Ratnakumar || Source Answers: Calling the function collections.namedtuple gives you a new type that’s a subclass of tuple (and no other classes) with a member …

Total answers: 7