Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Groovy equivalent to Python's dir()?

Tags:

python

groovy

In Python I can see what methods and fields an object has with:

print dir(my_object)

What's the equivalent of that in Groovy (assuming it has one)?

like image 704
Noel Yap Avatar asked Jun 04 '12 13:06

Noel Yap


People also ask

What is dir() Python?

Python dir() Function The dir() function returns all properties and methods of the specified object, without the values. This function will return all the properties and methods, even built-in properties which are default for all object.

How to use help and dir in Python?

Help() and dir(), are the two functions that are reachable from the python interpreter. Both functions are utilized for observing the combine dump of build-in-function. These created functions in python are truly helpful for the efficient observation of the built-in system.


1 Answers

Looks particulary nice in Groovy (untested, taken from this link so code credit should go there):

// Introspection, know all the details about classes :
// List all constructors of a class
String.constructors.each{println it}

// List all interfaces implemented by a class
String.interfaces.each{println it}

// List all methods offered by a class
String.methods.each{println it}

// Just list the methods names
String.methods.name

// Get the fields of an object (with their values)
d = new Date()
d.properties.each{println it}

The general term you are looking for is introspection.

like image 187
ChristopheD Avatar answered Oct 18 '22 00:10

ChristopheD