Differences between Groovy and Python
Content
- General
- Lists List
- Maps
- Ranges/Slices range/segment
- Object access
- References
Groovy is a JVM-based Agile development language that combines many powerful features of Python, Ruby, and Smalltalk. Groovy code can be well combined with Java code, it can also be used to expand existing code. Because of its running characteristics on 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
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/segment
Python |
Groovy |
Range (3) |
0 .. <3 |
Range (1, 3 + 1) |
1. 3 |
Range (0, 10, 2) |
Not represented as a data type but you can use 0. step (10, 2 ){...} |
"Abcdef" [3:] |
"Abcdef" [3...-1] |
Object access
Python |
Groovy |
M = 'strip'; getattr ('! ', M )() |
M = 'trim ';'! '. "$ M "() |
Args = ('A', 2); 'abc'. find (* args) |
Args = ['A', 2]; 'abc'. indexOf (* args) |
References
- Groovy Differences from Python