News for hackers. Stuff that matters.
Re:Wield Python's super() like a Jedi by uriel on Thursday May 26, 2011 @07:25PM (#2589822)
One of the reasons I'm starting to love Go more than Python.

In Go something like super() is superfluous.

Re:Wield Python's super() like a Jedi by jessedhillon on Thursday May 26, 2011 @11:58PM (#2590386)
The only reason why you don't need super in Go is because you cannot embed multiple interfaces except if their sets of methods are disjoint. This eliminates super's job of allowing programmers to differentiate between base classes with conflicting method implementations.

Without this, Go requires you to design your class hierarchy in a way that is cleaner, but the super concept is still not superfluous -- because you do sometimes need to reference an ancestor's implementation in Go.

You embed an interface and then explicitly reference the embedded interface by name. The Go interface

  type ReaderWriter interface {
    Reader
    Writer
  }
is exactly like this Python

  class ReaderWriter(Reader, Writer):
    pass
To get at a particular superclass implementation, you pass the class to super, like so:

  def read_and_write(self):
    super(Reader).read()
    super(Writer).write()
which in Go is:

  func (rw *ReaderWriter) ReadAndWrite() {
    rw.Reader.read()
    rw.Writer.write()
  }

So it's not superfluous -- there's an equivalent syntax to super which involves referencing embedded interfaces.
Re:Wield Python's super() like a Jedi by Peristarkawan on Friday May 27, 2011 @02:20AM (#2590603)
Sigh. No, that is not at all how super works in Python. The equivalent code to that Go function would be:

  def read_and_write(self):
      Reader.read(self)
      Writer.write(self)
That is, if you want to get at a particular superclass implementation, then you just call it directly. super() is used when you're not calling a particular superclass, instead relying on Python to compute the correct "next method" for you.

"super(Reader).read()", if you actually try it, will just result in an AttributeError. This is because super(Reader) returns an unbound super object, not a bound super object that you can actually dispatch from. Why this advanced usage might be useful is beyond the scope of this thread, but if you're curious then just google for python unbound super and hit the Feeling Lucky button.

Re:Wield Python's super() like a Jedi by jessedhillon on Friday May 27, 2011 @04:01AM (#2590765)
Yeah you're right, I was mistaken. super uses the MRO of the first argument, or inspects the stack if none is given.

BTW your sighing is not necessary.

Re:Wield Python's super() like a Jedi by Peristarkawan on Friday May 27, 2011 @01:13PM (#2592191)
I apologize. It was late, and my supply of patience for the day was exhausted.