The difference between Groovy and Python "translation"

Source: Internet
Author: User
Tags ranges

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"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.