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
164
votes
15 answers

Retrieving the inherited attribute names/values using Java Reflection

I've a Java object 'ChildObj' which is extended from 'ParentObj'. Now, if it is possible to retrieve all the attribute names and values of ChildObj, including the inherited attributes too, using Java reflection mechanism? Class.getFields gives me…
Veera
  • 32,532
  • 36
  • 98
  • 137
133
votes
4 answers

How do I get the filepath for a class in Python?

Given a class C in Python, how can I determine which file the class was defined in? I need something that can work from either the class C, or from an instance of C. I am doing this because I am generally a fan of putting files that belong together…
Staale
  • 27,254
  • 23
  • 66
  • 85
132
votes
4 answers

Get __name__ of calling function's module in Python

Suppose myapp/foo.py contains: def info(msg): caller_name = ???? print '[%s] %s' % (caller_name, msg) And myapp/bar.py contains: import foo foo.info('Hello') # => [myapp.bar] Hello I want caller_name to be set to the __name__ attribute of…
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
131
votes
5 answers

List all the modules that are part of a python package?

Is there a straightforward way to find all the modules that are part of a python package? I've found this old discussion, which is not really conclusive, but I'd love to have a definite answer before I roll out my own solution based on os.listdir().
static_rtti
  • 53,760
  • 47
  • 136
  • 192
130
votes
8 answers

How can you print a variable name in python?

Say I have a variable named choice it is equal to 2. How would I access the name of the variable? Something equivalent to In [53]: namestr(choice) Out[53]: 'choice' for use in making a dictionary. There's a good way to do this and I'm just missing…
physicsmichael
  • 4,793
  • 11
  • 35
  • 54
128
votes
8 answers

How do I access properties of a javascript object if I don't know the names?

Say you have a javascript object like this: var data = { foo: 'bar', baz: 'quux' }; You can access the properties by the property name: var foo = data.foo; var baz = data["baz"]; But is it possible to get these values if you don't know the name of…
Adam Lassek
  • 35,156
  • 14
  • 91
  • 107
125
votes
7 answers

Swift class introspection & generics

I am trying to dynamically create a class instance based type using generics, however I am encountering difficulty with class introspection. Here are the questions: Is there a Swift-equivalent to Obj-C's self.class? Is there a way to instantiate a…
Erik
  • 12,730
  • 5
  • 36
  • 42
121
votes
7 answers

Can you list the keyword arguments a function receives?

I have a dict, which I need to pass key/values as keyword arguments.. For example.. d_args = {'kw1': 'value1', 'kw2': 'value2'} example(**d_args) This works fine, but if there are values in the d_args dict that are not accepted by the example…
dbr
  • 165,801
  • 69
  • 278
  • 343
112
votes
13 answers

Get an object properties list in Objective-C

How can I get a list (in the form of an NSArray or NSDictionary) of a given object properties in Objective-C? Imagine the following scenario: I have defined a parent class which just extends NSObject, that holds an NSString, a BOOL and an NSData…
boliva
  • 5,604
  • 6
  • 37
  • 39
95
votes
5 answers

What is the C# equivalent to Java's isInstance()?

I know of is and as for instanceof, but what about the reflective isInstance() method?
diegogs
  • 2,036
  • 2
  • 16
  • 20
88
votes
5 answers

How do I check if a variable is an instance of a class?

In Java, you can do instanceof. Is there a Ruby equivalent?
NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352
88
votes
6 answers

How to list all fields of a class (and no methods)?

Suppose o is a Python object, and I want all of the fields of o, without any methods or __stuff__. How can this be done? I've tried things like: [f for f in dir(o) if not callable(f)] [f for f in dir(o) if not inspect.ismethod(f)] but these return…
Eric Wilson
  • 57,719
  • 77
  • 200
  • 270
88
votes
3 answers

What's the biggest difference between dir() and __dict__ in Python

class C(object): def f(self): print self.__dict__ print dir(self) c = C() c.f() output: {} ['__class__', '__delattr__','f',....] why there is not a 'f' in self.__dict__
shellfly
  • 892
  • 2
  • 13
  • 20
87
votes
5 answers

Get Activity name dynamically - android

I'd like to get the name of the current Activity to be sent along in the URI of an HttpRequest. Is there a way to do this without referring specifically to the Activity? I know I can do myActivity.class.toString() but this is just a less efficient…
evanmcdonnal
  • 46,131
  • 16
  • 104
  • 115
78
votes
7 answers

Objective-C Introspection/Reflection

Is there a built in method, function, API, commonly accepted way, etc. to dump the contents of an instantiated object in Objective-C, specifically in Apple's Cocoa/Cocoa-Touch environment? I want to be able to do something like MyType *the_thing =…
Alana Storm
  • 164,128
  • 91
  • 395
  • 599
1
2
3
68 69