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
1918
votes
12 answers

Getting the class name of an instance

How do I find out the name of the class used to create an instance of an object in Python? I'm not sure if I should use the inspect module or parse the __class__ attribute.
Dan
  • 33,953
  • 24
  • 61
  • 87
1448
votes
31 answers

Is there a built-in function to print all the current properties and values of an object?

So what I'm looking for here is something like PHP's print_r function. This is so I can debug my scripts by seeing what's the state of the object in question.
fuentesjr
  • 50,920
  • 27
  • 77
  • 81
720
votes
27 answers

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

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 better…
Rob
  • 7,420
  • 3
  • 17
  • 12
667
votes
22 answers

Finding what methods a Python object has

Given a Python object of any kind, is there an easy way to get the list of all methods that this object has? Or if this is not possible, is there at least an easy way to check if it has a particular method, other than checking if an error occurs…
Thomas Lötzer
  • 24,832
  • 16
  • 69
  • 55
597
votes
4 answers

Get all object attributes in Python?

Is there a way to get all attributes/methods/fields/etc. of an object in Python? vars() is close to what I want, but it doesn't work unless an object has a __dict__, which isn't always true (e.g. it's not true for a list, a dict, etc.).
user541686
  • 205,094
  • 128
  • 528
  • 886
563
votes
5 answers

Ruby: kind_of? vs. instance_of? vs. is_a?

What is the difference? When should I use which? Why are there so many of them?
Claudiu
  • 224,032
  • 165
  • 485
  • 680
500
votes
7 answers

How to get the name of the current method from code

I know you can do this.GetType().FullName To get My.Current.Class But what can I call to get My.Current.Class.CurrentMethod
Nick Allen
  • 11,970
  • 11
  • 45
  • 58
368
votes
28 answers

How do I look inside a Python object?

I'm starting to code in various projects using Python (including Django web development and Panda3D game development). To help me understand what's going on, I would like to basically 'look' inside the Python objects to see how they tick - like…
littlejim84
  • 9,071
  • 15
  • 54
  • 77
349
votes
20 answers

How to get method parameter names?

Given that a function a_method has been defined like def a_method(arg1, arg2): pass Starting from a_method itself, how can I get the argument names - for example, as a tuple of strings, like ("arg1", "arg2")?
Staale
  • 27,254
  • 23
  • 66
  • 85
304
votes
7 answers

How do you print out a stack trace to the console/log in Cocoa?

I'd like to log the call trace during certain points, like failed assertions, or uncaught exceptions.
robottobor
  • 11,595
  • 11
  • 39
  • 37
280
votes
3 answers

What does the slash mean when help() is listing method signatures?

What does the / mean in Python 3.4's help output for range before the closing parenthesis? >>> help(range) Help on class range in module builtins: class range(object) | range(stop) -> range object | range(start, stop[, step]) -> range object |…
Joschua
  • 5,816
  • 5
  • 33
  • 44
278
votes
12 answers

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

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 caller…
zs2020
  • 53,766
  • 29
  • 154
  • 219
245
votes
5 answers

Using isKindOfClass with Swift

I'm trying to pick up a bit of Swift lang and I'm wondering how to convert the following Objective-C into Swift: - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [super touchesBegan:touches withEvent:event]; UITouch *touch…
lkemitchll
  • 2,751
  • 2
  • 15
  • 16
191
votes
7 answers

List all base classes in a hierarchy of given class?

Given a class Foo (whether it is a new-style class or not), how do you generate all the base classes - anywhere in the inheritance hierarchy - it issubclass of?
Sridhar Ratnakumar
  • 81,433
  • 63
  • 146
  • 187
174
votes
12 answers

Get model's fields in Django

Given a Django model, I'm trying to list all of its fields. I've seen some examples of doing this using the _meta model attribute, but doesn't the underscore in front of meta indicate that the _meta attribute is a private attribute and shouldn't be…
Joe J
  • 9,985
  • 16
  • 68
  • 100
1
2 3
68 69