News for hackers. Stuff that matters.
Re:Five-minute Multimethods in Python by SammyRulez on Wednesday December 07, 2011 @08:37AM (#3323654)
The power of a static typing in a dynamic typed language.. that's great.. and elegant too! GO BDFL !
Re:Five-minute Multimethods in Python by radarsat1 on Wednesday December 07, 2011 @09:20AM (#3323839)
Actually it's just checking the types at runtime, nothing much 'static' about it. This is pretty much the antithesis of duck typing, in fact. I could see it being useful, but I could also see it leading to confusion for people who expect Python's usual duck-typing behaviour when they pass their own types into a function.

Whoops you subclassed from a type for which this function has a multimethod defined? Gonna do something different.

On the other hand, in non-trivial programs there _are_ certainly occasions when you need to check the type, so using an elegant method like this isn't a bad compromise.

Re:Five-minute Multimethods in Python by tikhonj on Wednesday December 07, 2011 @08:47AM (#3323699)
Except that you have to statically specify the types using decorators. So it's more like a subset of static typing in a dynamically typed language.
Re:Five-minute Multimethods in Python by skimbrel on Wednesday December 07, 2011 @10:30AM (#3324151)
It's not a subset of static typing at all.

If multimethods were a subset of static typing, then regular methods would be too. Methods are dynamic by definition: the type of the method's target is inspected at runtime and used to dispatch to the correct function entry. In Python, it's explicitly stated because the first argument of all object methods is "self". Multimethods extend this to do dynamic dispatch on all argument types, not just the target object.

Most Python object methods are declared at compile time; this doesn't make method declaration a "subset of static typing". You can add new methods to classes and objects at runtime using Python's reflection and introspection; you can just as easily add new multimethods at runtime using this framework.

Finally, the @decorator(...) syntax at compile time is just syntactic sugar for something like the following:

  def _foo(self, arg):
    ...
  foo = decorator()(_foo)
So you can use decorators at runtime whenever you want too.