News for hackers. Stuff that matters.

tef__

Re:Ruby is beautiful (but I'm moving to Python) on Sunday November 28, 2010 @01:49PM (#1947892)
To some extent: the underscore methods represent 'magic behaviour' i.e. this gets called implicitly by something else, you never call this directly.

i.e you write 4+2 to add things not 4.add(2)

Re:Ruby is beautiful (but I'm moving to Python) on Sunday November 28, 2010 @01:48PM (#1947887)
'I love that I can define + for any object in Ruby and that doing so gives me += for free.'

fwiw: this works in python too

>>> class Foo(object): ... def __add__(self, x): ... print "%s+%s"%(self,x) ... >>> a = Foo() >>> a+=8 <__main__.Foo object at 0x657d0>+8 >>>

You can override __iadd__ separately too.

Re:Ruby is beautiful (but I'm moving to Python) on Sunday November 28, 2010 @01:45PM (#1947877)
the thing is, self being explicit makes it more consistent

unlike 'self' in other languages as a keyword, 'self' is just a variable with the same scoping.

i.e self always refers to the most inner scope in other languages (a special variable), meanwhile self in python is wherever self was bound in the current scope.

and fwiw: len is a holdover from earlier python (you can define a method __len__ if you want...)