Live data from Hacker News

Making Python's __init__ method magical

blog.lerner.co.il

41–42 of 42 posts

Re: Making Python's __init__ method magical

#41
post #25
post #5

CoffeeScript has a nice syntactic sugar for setting properties by prefixing the argument name with "@": class Foo constructor: (@x, @y) -> @z = @x + @y setFoo: (@foo) -> Which compiles to: http://coffeescript.org/#try:class%20Foo%0A%20%20constructor...

Interesting you mentioned CoffeeScript and syntactic sugar, because adding an equivalent "self." in python inits' parameter names doesn't work the same way in CoffeeScript. def __init__(self, self.x, self.y): # fail pass Did you mention this because there is an existing PEP that proposes this functionality?

No, I just thought the way CoffeeScript handles it is pretty slick. I looked around, and it seems like proposals to add something like this to Python did come up [1] [2], but weren't very welcomed.

[1] https://mail.python.org/pipermail/python-dev/2005-July/05456...

[2] http://bytes.com/topic/python/answers/101594-__autoinit__-pr...

Re: Making Python's __init__ method magical

#42

Really really minor nitpick, but when talking about languages and their traits these little details can become important for the reader to understand, lest it confuse them about other languages: > While many people think of __init__ as a constructor, that’s not really the case. Rather, __init__ is invoked on our new object after it has been created I'm not aware of a language (though I don't claim to know them all!)…

So, we've got three roles and three names... just let's call the allocator "allocator", the initializer "initializer", and the thing which does both the "constructor", maybe? :)
Post reply on HN