Ian Bicking: the old part of his blog

Re: First Try at Generic Functions

It's been a little bit since I've used them (and PJE can correct me if this is not the "preferred" way), but instead of:

@jsonify.when('isinstance(obj, Binary)')
...

you can do:

@jsonify.when('obj in Binary')
...

Saves a little typing, but the meaning isn't immediately obvious to people that don't know the RuleDispatch package (of course, anyone trying to look at code using RuleDispatch without understanding RuleDispatch will have problems anyway)

Comment on First Try at Generic Functions
by Jay P.

Comments:

Since (I don't think) obj in Binary will ever be generally equivalent to isinstance(obj, Binary) I wouldn't want to use that. I know I'm not necessarily Typical, but generic functions make a lot of intuitive sense to me, and as long as they don't use funny special constructs like this I think other people can pick them up quite easily as well. And RuleDispatch code reads pretty well, which is something I particularly like, and something that should make the resulting code accessible to people who have little understanding of RuleDispatch. I didn't read up about it at all before using it -- except to get the import right -- so I don't think you need a thorough understanding to start. But then I've read about it a while ago, so I might have lingering understanding that I've forgotten about.

# Ian Bicking

At work we've been using:

@jsonify.when(Binary)
...

-Chris

# Chris L

That only works with @dispatch.on, not @dispatch.generic, unless you use a dispatch.strategy.PositionalSignature with a sequence of classes, not a single class. E.g.:

from dispatch.strategy import PositionalSignature

@jsonify.when(PositionalSignature([Binary]))
def jsonify(ob):
    # ...

As you can see, this is more trouble than it's worth, unless you're writing code that dynamically adds methods and doesn't want to have to do string manipulation:

def register_jsonifier(cls, func):
    @jsonify.when(PositionalSignature([cls]))
    def foo(ob): return func(ob)

However, because of the way condition strings are interpreted, you could just as easily write:

def register_jsonifier(cls, func):
    @jsonify.when("isinstance(ob,cls)")
    def foo(ob): return func(ob)

Because the reference to cls is resolved immediately and treated as a constant thereafter. So it's really pretty rare that you'd want to delve into the object-based interface to the criteria system.

# Phillip J. Eby