From: Doug H Date: 2005-12-21T08:42:49+09:00 Subject: Re: Bruce Eckel wouldn't know why to switch from Python to Ruby gabriele renzi wrote: > rcoder ha scritto: > > Eckel's article is getting pretty long in the tooth at this point -- I > > remember reading it back when I was a Python user several years ago, > > and it actually delayed my swith to Ruby for a few months, at least. I > > probably don't qualify as a Python expert, but after being exposed to > > both for non-trivial projects, there are definitely some differences > > that make me prefer Ruby. > > > > First, because all object "slots" in Python are public, (stupid > > conventions like underscore prefixes aside) you see a lot of code like > > this: > > > > obj.setFoo("bar") > > > > This is because 'obj.foo = "bar"' is a direct assignment, with no > > method call involved. > > I think you'have been away from python far too long, recent python (3 or > 4 versions I think_) has a nice thing called properties. > Basically you can declare "foo" to have setters and getters when you > need it, and still threat them in the same way (i.e. "obj.foo = bar" > becomes a method call). 80% of the times someone writing a setFoo or > getFoo is doing it because (s)he was a java/c++ developer. Properties in python don't work with subclasses, however. #python: class A(object): def __init__(self): self.x = 0 def getX(self): return self.x def setX(self, val): self.x = val X = property(fget=getX, fset=setX) class B(A): def getX(self): return self.x + 1 b = B() print b.X # 0 instead of 1, uh oh ####################### #ruby: class A attr_accessor :X def initialize @X = 0 end end class B