Hacker Newsnew | past | comments | ask | show | jobs | submit | dstywho's commentslogin

One of the things that bothers me about python is the way you have to specify 'self' for instance methods. Instance methods should be the norm not the exception.


No, it's awesome.

It makes "morphing code", such as decorators easier to write. It's explicit. It's consistent (with class methods for instance). And it's not some special case magic with special syntax. It's just another parameter.

Python's great strengths is avoiding special case magic. Much more than many people realize as there's lots of syntactic sugar on top of double underscore functions and interfaces like context manager and generators.


Interestingly you can do this:

    class Foo:
        def bar(self):
            return "bar"

    foo = Foo()
    foo.bar()      # returns "bar"
    Foo.bar(foo)   # returns "bar"
So it's not actually specifying `self` for instance methods; it's just a special thing about class instances that calling a method will call its class's method passing the instance as the first argument.

After realizing this, I was enlightened.


> Foo(foo, bar) # returns "bar"

I think you meant `Foo.bar(foo)` here.


You're right. I'm not thinking.


Methods are type specialized functions, just syntactic sugar for calling functions with a context (provided by an instance of the required type) that you defined elsewhere.

    class Foo(object):
        def __init__(self, bar):
            self.bar = bar
        def something(self, arg):
            return [self.bar, arg]
I can do this:

    assert(Foo('spam').something('eggs') == Foo.something(Foo('spam'), 'eggs')
But I could also do this:

   assert(Foo('spam').something(Foo('ham'), 'eggs') == ['ham', 'eggs'])


I read this while I was writing my TPS cover sheet.


What if your boss reads Hacker News?...Success!


They did wait to post it until they had already quit.


because Hibernate is awesome!!


closures yet?


Not yet. Closures and other features were delayed to get Java 7 out sooner.

http://openjdk.java.net/projects/jdk7/features/



i don't want java doc :D; i want rdoc.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: