News for hackers. Stuff that matters.

Peristarkawan

Re:Wield Python's super() like a Jedi on Friday May 27, 2011 @01:13PM (#2592191)
I apologize. It was late, and my supply of patience for the day was exhausted.
Re:Wield Python's super() like a Jedi 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 on Friday May 27, 2011 @01:57AM (#2590566)
I mistyped. I meant "subclass OrderedCounter to add in the __missing__ method".
Re:Wield Python's super() like a Jedi on Friday May 27, 2011 @01:54AM (#2590564)
Not really. The only thing that changed about super in Python 3 is that you can call it with no arguments, which is just a short-hand for the way it was already used in Python 2. Everything in that article applies equally to both Python versions.
Re:Wield Python's super() like a Jedi on Thursday May 26, 2011 @06:46PM (#2589704)
The version that I was rewriting:

  def __init__(self, shapename=None, **kwds):
also allows that.
Re:Wield Python's super() like a Jedi on Thursday May 26, 2011 @04:49PM (#2589389)
That's true. The second one might be better written as:

  def __init__(self, shapename, **kwds):
Or, in Python 3:

  def __init__(self, *, shapename, **kwds):
(making shapename a required keyword-only argument)
Re:Wield Python's super() like a Jedi on Thursday May 26, 2011 @03:04PM (#2589012)
That's entirely the caller's fault, not the function's. Compare:

  >>> def init(**kwargs):
  ...   pass
  ...
  >>> init(shapename='circle', **{'shapename': 'circle'})
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  TypeError: init() got multiple values for keyword argument 'shapename'
Re:Wield Python's super() like a Jedi on Thursday May 26, 2011 @02:35PM (#2588908)
I disagree, that's extremely fragile. It might work for the particular class you are implementing. However, it might cause subclasses of that class to break, since those subclasses will have a different MRO than the current class, and you could end up skipping more of the MRO than you intended.
Re:Wield Python's super() like a Jedi on Thursday May 26, 2011 @02:29PM (#2588890)
The reason for using keywords is that there is no guarantee that the next class in the MRO after ColoredShape will be Shape. When the instance is a ColoredShape, it will be, but if ColoredShape is subclassed with a more complex inheritance structure, then something else could end up in between them in the MRO.

Thus, ColoredShape.__init__ can't simply pass on shapename to the super method, because that might not be the argument expected by the next __init__ method in the MRO. Instead, each method needs to gracefully accept arbitrary arguments and then pass on whatever it received (minus what it consumed).

Re:Wield Python's super() like a Jedi on Thursday May 26, 2011 @02:09PM (#2588785)
Okay, I give. What's the markup for posting code snippets on this site?
Re:Wield Python's super() like a Jedi on Thursday May 26, 2011 @02:01PM (#2588756)
It seems to me that the example of combining built-in dictionary classes is naively optimistic. For starters, OrderedDict, as it happens, does not use super! It calls the dict super-class methods directly. Since dict happens to be the next class in the MRO, this doesn't really matter for the purpose of this example, but I can envision a scenario where some plucky programmer inherits from both OrderedCounter and some other dict subclass, and the result doesn't work because OrderedDict does the wrong thing.

And OrderedDict isn't the only one. Maybe for some reason I would like to have an OrderedCounter where all the counts default to 42. So I do this:

  class DefaultOrderedCounter(defaultdict, OrderedCounter):
      pass
  doc = DefaultOrderedCounter(lambda: 42)
  doc.update('abracadabra')
Which results in:

  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "c:\python32\lib\collections.py", line 507, in update
      _count_elements(self, iterable)
    File "c:\python32\lib\collections.py", line 63, in __setitem__
      self.__map[key] = link = Link()
  AttributeError: 'DefaultOrderedCounter' object has no attribute '_OrderedDict__map'
Whoops! Apparently defaultdict doesn't use super either. Of course a better way to do this would be to subclass DefaultOrderedCounter and just override the __missing__ method by hand, but that's not the point.

The article goes into "How to Incorporate a Non-cooperative Class", which basically says "wrap it up in a proxy class". But that's not really going to work here, since the result would be two separate dicts, with the defaultdictwrapper methods operating on one dict, and the other methods operating on the other.

Re:Wield Python's super() like a Jedi on Thursday May 26, 2011 @01:50PM (#2588710)
Yes, Python's super is more like Dylan's next-method than, say, C#'s super.