Better HN
topnewbestaskshowjobs
Log inSign up

tef__

11 karma · joined Nov 28, 2010 (16y ago) · 3 submissions

Submissions

comment 16y ago · link · parent

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)

comment 16y ago · link · parent

'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.

comment 16y ago · link · parent

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...)