• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Some Groovy vs Python observations.

 
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I remeber asking Josh to do the comparison. Finally decided to check it out myself.

(G) - Groovy
(P) - Python

New Line
(G)
println
(P)
print

Object Instantition: Python does'nt need 'new'
(G)
h = new HelloWorld()
(P)
h = HelloWorld()

Lists: Same as lists in python and are first class.
(G)
list = [1,2,'hello', new HelloWorld()]
(P)
list = [1,2,'hello', HelloWorld()]

Empty Lists are denoted the same way:

(G)
list =[]
(P)
list =[]

Map: dictionaries in python and are first class. P uses '{' instead of a '['
(G)
dict = ['name':'python','oo':'yes']
(P)
dict = {'name':'python','oo':'yes'}

Empty Map:
(G)
dict = [:] //dont have a choice here because [] mean empty lists.
(P)
dict = {}

Iteration: Same as Python. Dont need '(' and ')' in Python though.
'for' iterates on any sequence (list, string, map)

(G)
for (i in list): {println i}
(P)
for i in list: print i

for i in list:
# do something
else:
# do something else. # Optional. Gets executed when there is no abrupt exit while iterating.

Iterating a Map. Python iterates through the keys, while groovy returns

'flatten' method on list flattens nested lists if any, within a list: Missing in Python lists?! suprising!

(G)
a = [1,2,3,[4,5,6]]
a.flatten()
print a

groovy> a.flatten()
[1, 2, 3, 4, 5, 6]

join method: is a list method in groovy. Its a string method in Python.
'join' the contents of the list using a separator --> uhhm , join() does make more sense on 'list'
object then string.

(G)
l = ['h','e','l','l','o']
l.join('')

groovy> l.join('')
"hello"
(P)
>>> l = ['h','e','l','l','o']
>>> ''.join(l)
'hello'
tokenize method: Same as 'split' in Python. Returns a list with the tokens
(G)
groovy> 'java|python|perl'.tokenize('|')
["java", "python", "perl"]
(P)
>>> 'java|python|perl'.split('|')
['java', 'python', 'perl']


control structures : if - else.
if-in conditional for lists and maps does not work in Groovy.?! I thought that was nice.

(G)
if (x > 2){
//do this
}else{
// do that
}


(P)if x > 2:
#do this
else:
# do that

map = {'orm':'hibernate','mvc':'struts'}
list = ['java','groovy','python']

if 'orm' in map:
print map['orm'] # hibernate. looking up a key not present in a dict results in an error.
if 'java' in list:
# do something

Switch statements: Groovy - Yes
Python - No

Operator Overloading: Design is strikingly similar to that of Python's

Discussed in the special methods section below

Functions are objects: They can be passed around like any other normal object. Groovy is same as Python here.
in Python they are of type 'FunctionType' or 'MethodType' etc depending upon whether the function is a
builtin / not. In Groovy, function objects are of type org.codehaus.groovy.runtime.MethodClosure.
They are callable.

(G)
class Hello{
sayHello(){
return "hello world"
}

invokeThis(obj){
println " I have been asked to say " + obj())
}

}
h = new Hello()
f = h.sayHello
h.invokeThis(f) # " I have been asked to say hello world

This one is interesting ..Groovy Closures:

I'm not *very* comfortable yet. But initial impressions are that it looks like 'lambda's in Python?

The following example in Groovy

(G)
x = {a,b | println a+b};
x(10,20)
groovy> x(10,20);
>30

would translate to the following in Python

(P)
x = lambda x,y: print x+y;
x(10,20)
>> 30

Are lambdas same as closure?. The fact that they can be passed around they can be definitely be used as
'callbacks'. We define interfaces in java in order to do the same. Infact, the examples are so
reminiscent of Spring APIs...for eg Spring JdbcTemplate.So I like this feature. Good programming practices
should be embedded in the language itself.


Special methods on objects: Again the design is same as Python's:

Python allows for special methods on custom types. So the custom types can enjoy behaviour similar
to built in types. Python invokes those special methods when we access such a behaviour. They generally start wit
__ and end with a __

So,

(G)
if a & b are instances of the class Hello,
a + b maps to a.plus(b)
a - b maps to a.minus(b)
a * b maps to a.multiply(b)
a / b maps to a.divide(b)
a[b] maps to a.get(b)

(P)
In Python,
a + b maps to a.__add__(b)
a - b maps to a.__sub__(b)
a * b maps to a.__mul__(b)
a / b maps to a.__div__(b)
a[b] maps to a.__getitem__(b)

Eg:
class Money:
def __init__(self,amt):
self.amt = amt

def __add__(self,anotherM):
return Money(self.amt +anotherM.amt)

m1 = Money(10)
m2 = Money(20)
m3 = m1 + m2 # translates to m1.__add__(m2)
print m3.amt # 30


Documentation:

(G)
BAD. Finding information was'nt very easy.
Hopefully, will improve with a final release...not sure when though? It is currently beta 10

(P)
GREAT. Not surprising its been around for more than a decade.

Libraries: If Java has it, then so does Groovy.
I'm not a python regular and hence am not very conversant with Python Libraries.

Its obvious that this comparison is by no means comprehensive. But I downloaded groovy just yesterday night and I just wanted to get started. I might be wrong in certain places. Please do correct me. I figured couple of other things too but will update sometime later. Shall post my observations if you guys find it to be of any use at all.

I dont know anything about groovy performance. I guess they do get compiled to bytecodes.Once they have a final release, I do not see any reason why I should be using any other language. I was writing some sample code to get a hang of groovy and quickly realized that Groovy has access to *every* java api. I saw the movie 'Aliens' again yesterday ( i loved that movie ) after a long time. The alien would breed inside the human and eventually get rid of the host. I dont know why but i see a parallel here..LOL. Assuming performance is not an issue, its does not look like java vs c# anymore. Depending upon one's passion for java, it appears that we have a bigger danger lurking in our own backyard.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a glaring mistake in my post. I just figured that lambdas are not as powerful as closures. A groovy closure can have multiple statements and expressions while you can have only expressions in a python lambda. So all lambdas are definitely closures but its not true the other way around.
I think i spoke too soon. Now that i'm digging a little deeper, Groovy does not look as dynamic as python and it does not let you into its secrets like Python does. Right now i dont understand a whole lot about inner workings of groovy..but Python does not hide things.

and btw, all my indentation etc disappeared when i did a cut and paste from wordpad on to the brower while posting. How do you guys manage to make long posts readable ( with tabs, paras, highlighting etc?)
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Karthik Guru:
btw, all my indentation etc disappeared when i did a cut and paste from wordpad on to the brower while posting. How do you guys manage to make long posts readable ( with tabs, paras, highlighting etc?)


Use the code... Also, some forums support a subset of HTML, I think.
 
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does anybody know if groovy has generators? i'm not yet very good at using them, but i've already found them very handy, and Python 2.4's generator expressions have me drooling to upgrade.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M Beck:
does anybody know if groovy has generators? i'm not yet very good at using them, but i've already found them very handy, and Python 2.4's generator expressions have me drooling to upgrade.



yeah this looks cool. Thanks for pointing out. I dont kno whow i will be using it though.
Anyways, it does'nt look like Groovy has python like generators.

Looks like groovy's closure comes close. I mean a generator is about remebering the snapshot / local variable values at the point we do a 'yield' and resume again when next() is called right?

A groovy closure sure does retain the snapshot. But a simple Python generator seems to remeber the local variables when running through a loop!




Groovy



But I'm not able to write the python for loop example using groovy closure.
May be its just me.
 
M Beck
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ah, in that case you probably could replace a generator with a closure. to emulate the for loop, you just need a conditional in the closure to detect when the "for loop" should exit, and raise the appropriate exception at that point. neat!

and as for how to use generators - they're really handy for creating complex sequences, ones complicated enough that you'd need a separate function to compute each new item in the sequence, especially when you want to create them one item at a time so as to save space and/or time. python 2.4's generator expressions wrap this usage up neatly in some very readable syntax.
 
Karthik Guru
Ranch Hand
Posts: 1209
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by M Beck:
ah, in that case you probably could replace a generator with a closure. to emulate the for loop, you just need a conditional in the closure to detect when the "for loop" should exit, and raise the appropriate exception at that point. neat!



What i meant was , groovy does not support yield out of the box. I mean the ability to feeze a function at any aribitrary point of execution and resume from that point again. Do you still think that closures provide what generators do.


 
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is a great comparison! I apologize for never getting a comparison together, but I am still learning Python. I am currently reading a Python tutorial and I have a "Jython for Java Developers" book on the way to my house. I have not yet felt comfortable with comparing the two languages, but I feel that you have done an excellent job. I can use this post to help me in my studies as well.

Once again...apologies on never re-posting, but I am still learning the language.

Josh
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
python can delete and add functions to a class dynamicly,
can groovy? This is what I am care about.
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic