Content of this article
- General
- Lists List
- Maps maps
- Ranges/slices Range/Fragment
- Object Access Object
- Resources
Groovy is a JVM-based Agile development language that combines many of the powerful features of Python, Ruby, and Smalltalk, and groovy code can be well combined with Java code and can be used to extend existing code. Because of its features running on the JVM, Groovy can use libraries written in other Java languages.
General
Python |
Groovy |
REPR (x) |
X.inspect (), X.dump () |
x.y if x else None |
X?. Y |
"% (foo) s"% locals () |
"${foo}" |
Lists List
Python |
Groovy |
Not X |
!x X.empty |
Len (x) |
X.size () |
For item, IDX in enumerate (x): ... |
X.eachwithindex {Item, IDX--...} |
Maps maps
Python |
Groovy |
{} |
[:]//An empty map |
Depends: D if used like:for K in D: List (d) if list needed D[iter].keys () explicitly |
D.keyset () |
D.[iter]values () |
D.values () |
[K+1 for K in d] |
D.collect {k, V-k+1} |
D = dict (Zip (k, v)) |
K = 1..3 v = ' a ' ... ' C D = [:]; K.eachwithindex {It, I, d[it] = v[i]} println d//[1: "A", 2: "B", 3: "C"] |
Ranges/slices Range/Fragment
Python |
Groovy |
Range (3) |
0..<3 |
Range (1, 3+1) |
1..3 |
Range (0, 10, 2) |
Not represented as a data type if you can use 0.step (10, 2) {...} |
"ABCdef" [3:] |
"ABCdef" [3..-1] |
Object Access Object
Python |
Groovy |
m = ' strip '; GetAttr ('! ', m) () |
m = ' trim '; ‘ ! ‘." $m "() |
args = (' A ', 2); ' Abcabc '. Find (*args) |
args = [' A ', 2]; ' Abcabc '. IndexOf (*args) |
Resources
- Groovy Differences from Python
The difference between Groovy and Python "translation"