Questions tagged [introspection]

A capability of some object-oriented programming languages to determine the type of an object at runtime.

Some OOP languages providing Type Introspection ability : Ruby, Objective-C, C++, Java, PHP, Perl, Python, Object Pascal, Actionscript (as3)

The introspection is done differently, depending on the form of program, it is performed upon: inspecting a source code or compiled byte-code is called compile-time introspection, while inspecting properties of running code is a run-time introspection.

For example, Java supports both types of introspection: users of run-time introspection typically perform it via , while compile-time introspection can be implemented with tools or .

http://en.wikipedia.org/wiki/Type_introspection

1021 questions
37
votes
10 answers

How to get current function into a variable?

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…
Ryan C. Thompson
  • 40,856
  • 28
  • 97
  • 159
37
votes
4 answers

How to introspect django model fields?

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 =…
Evgeny
  • 10,698
  • 9
  • 60
  • 70
36
votes
6 answers

Get the name (string) of a generic type in Swift

I have a generic class of type T and I would like to get the name of the type that passed into the class when instantiated. Here is an example. class MyClass { func genericName() -> String { // Return the name of T. } } I have…
Rob
  • 4,149
  • 5
  • 34
  • 48
36
votes
7 answers

Get kwargs Inside Function

If I have a python function like so: def some_func(arg1, arg2, arg3=1, arg4=2): Is there a way to determine which arguments were passed by keyword from inside the function? EDIT For those asking why I need this, I have no real reason, it came up in…
Mark
  • 106,305
  • 20
  • 172
  • 230
32
votes
7 answers

How to introspect all available methods and members of a Rust type?

Is there a way to print out a complete list of available members of a type or instance in Rust? For example: In Python, I can use print(dir(object)) In C, Clang has a Python API that can parse C code and introspect it. Being unfamiliar with Rust…
ideasman42
  • 42,413
  • 44
  • 197
  • 320
32
votes
9 answers

Call private methods and private properties from outside a class in PHP

I want to access private methods and variables from outside the classes in very rare specific cases. I've seen that this is not be possible although introspection is used. The specific case is the next one: I would like to have something like…
Pablo López Torres
  • 1,083
  • 1
  • 8
  • 14
31
votes
5 answers

Print all local variables accessible to the current scope in Lua

I know how to print "all" global variables using the following code for k,v in pairs(_G) do print("Global key", k, "value", v) end So my question is how to do that for all variables that are accessible from the currently executing function,…
Edu Felipe
  • 10,197
  • 13
  • 44
  • 41
31
votes
3 answers

C# "is" operator - is that reflection?

A colleague asked me an interesting question today - is the C# keyword/operator "is" considered reflection? object tmp = "a string"; if(tmp is String) { } How is this operator implemented behind the scenes? Does it require reflection or…
Matt
  • 41,216
  • 30
  • 109
  • 147
29
votes
3 answers

What is the correct way to override the __dir__ method?

This question is meant to be more about __dir__ than about numpy. I have a subclass of numpy.recarray (in python 2.7, numpy 1.6.2), and I noticed recarray's field names are not listed when diring the object (and therefore ipython's autocomplete…
shx2
  • 61,779
  • 13
  • 130
  • 153
29
votes
4 answers

How can I list the methods in a Python 2.5 module?

I'm trying to use a Python library written in C that has no documentation of any kind. I want to use introspection to at least see what methods and classes are in the modules. Does somebody have a function or library I can use to list the functions…
Evan Kroske
  • 4,506
  • 12
  • 40
  • 59
28
votes
5 answers

Checking Objective-C block type?

This is primarily a curiosity, I'm not really sure what's the practical use of this but here goes. Since blocks are also Objective-C objects, is it possible to check their type? That is, does it respond to the isKindOfClass: message and how to use…
adib
  • 8,285
  • 6
  • 52
  • 91
27
votes
5 answers

How do I find all the property keys of a KVC compliant Objective-C object?

Is there a method that returns all the keys for an object conforming to the NSKeyValueCoding protocol? Something along the lines of [object getPropertyKeys] that would return an NSArray of NSString objects. It would work for any KVC-compliant…
armahg
  • 739
  • 2
  • 10
  • 11
27
votes
2 answers

Best way to add attributes to a Python function

Take the simple case of a Python function which evaluates a mathematical function: def func(x, a, b, c): """Return the value of the quadratic function, ax^2 + bx + c.""" return a*x**2 + b*x + c Suppose I want to "attach" some further…
xnx
  • 24,509
  • 11
  • 70
  • 109
27
votes
6 answers

What is the equivalent of /proc/cpuinfo on FreeBSD v8.1?

What is the equivalent of Linux's /proc/cpuinfo on FreeBSD v8.1? My application reads /proc/cpuinfo and saves the information in the log file, what could I do to get similar information logged on FreeBSD? A sample /proc/cpuinfo looks like…
WilliamKF
  • 41,123
  • 68
  • 193
  • 295
27
votes
5 answers

Checking for member existence in Python

I regularly want to check if an object has a member or not. An example is the creation of a singleton in a function. For that purpose, you can use hasattr like this: class Foo(object): @classmethod def singleton(self): if not…
PierreBdR
  • 42,120
  • 10
  • 46
  • 62