views:

75

answers:

3

I'd like to do something like this:

class SillyWalk(object):
    @staticmethod
    def is_silly_enough(walk):
        return (False, "It's never silly enough")
    def walk(self, appraisal_method=is_silly_enough):
        self.do_stuff()
        (was_good_enough, reason) = appraisal_method(self)
        if not was_good_enough:
            self.execute_self_modifying_code(reason)
        return appraisal_method
    def do_stuff(self):
        pass
    def execute_self_modifying_code(self, problem):
        from __future__ import deepjuju
        deepjuju.kiss_booboo_better(self, problem)

with the idea being that someone can do

>>> silly_walk = SillyWalk()
>>> appraise = walk()
>>> is_good_walk = appraise(silly_walk)

and also get some magical machine learning happening; this last bit is not of particular interest to me, it was just the first thing that occurred to me as a way to exemplify the use of the static method in both an in-function context and from the caller's perspective.

Anyway, this doesn't work, because is_silly_enough is not actually a function: it is an object whose __get__ method will return the original is_silly_enough function. This means that it only works in the "normal" way when it's referenced as an object attribute. The object in question is created by the staticmethod() function that the decorator puts in between SillyWalk's is_silly_enough attribute and the function that's originally defined with that name.

This means that in order to use the default value of appraisal_method from within either SillyWalk.walk or its caller, we have to either

  • call appraisal_method.__get__(instance, owner)(...) instead of just calling appraisal_method(...)
  • or assign it as the attribute of some object, then reference that object property as a method that we call as we would call appraisal_method.

Given that neither of these solutions seem particularly Pythonic™, I'm wondering if there is perhaps a better way to get this sort of functionality. I essentially want a way to specify that a method should, by default, use a particular class or static method defined within the scope of the same class to carry out some portion of its daily routine.

I'd prefer not to use None, because I'd like to allow None to convey the message that that particular function should not be called. I guess I could use some other value, like False or NotImplemented, but it seems a) hackety b) annoying to have to write an extra couple of lines of code, as well as otherwise-redundant documentation, for something that seems like it could be expressed quite succinctly as a default parameter.

What's the best way to do this?

+1  A: 

Not sure if I get exactly what you're after, but would it be cleaner to use getattr?

>>> class SillyWalk(object):
    @staticmethod
    def ise(walk):
        return (False, "boo")
    def walk(self, am="ise"):
        wge, r = getattr(self, am)(self)
        print wge, r


>>> sw = SillyWalk()
>>> sw.walk("ise")
False boo
Ryan Ginstrom
could be a workaround, I guess. I'd like to be able to override the default by passing a callable, and I'd also like the default case to be the fastest. Also it's not quite self-evident that "ise" is meant to be the name of a static method. I guess I'm looking for a function named something like `undecorate_static_method`. Which I suppose I could write, and include with every module that I wanted to use this pattern in.. so as to be able to make the `def` line `def walk(self, am=undecorate_static_method(ise)):`
intuited
A: 

I ended up writing an (un)wrapper function, to be used within function definition headers, eg

def walk(self, appraisal_method=unstaticmethod(is_silly_enough)):

This actually seems to work, at least it makes my doctests that break without it pass.

Here it is:

def unstaticmethod(static):
    """Retrieve the original function from a `staticmethod` object.

    This is intended for use in binding class method default values
      to static methods of the same class.

    For example:
        >>> class C(object):
        ...     @staticmethod
        ...     def s(*args, **kwargs):
        ...         return (args, kwargs)
        ...     def m(self, args=[], kwargs={}, f=unstaticmethod(s)):
        ...         return f(*args, **kwargs)
        >>> o = C()
        >>> o.s(1, 2, 3)
        ((1, 2, 3), {})
        >>> o.m((1, 2, 3))
        ((1, 2, 3), {})
    """
    # TODO: Technically we should be passing the actual class of the owner
    #         instead of `object`, but
    #         I don't know if there's a way to get that info dynamically,
    #         since the class is not actually declared
    #         when this function is called during class method definition.
    #       I need to figure out if passing `object` instead
    #         is going to be an issue.
    return static.__get__(None, object)

update:

I wrote doctests for the unstaticmethod function itself; they pass too. I'm still not totally sure that this is an actual smart thing to do, but it does seem to work.

intuited
+1  A: 

Maybe all you need is to use the function (and not the method) in the first place?

class SillyWalk(object):
    def is_silly_enough(walk):
        return (False, "It's never silly enough")

    def walk(self, appraisal_function=is_silly_enough):
        self.do_stuff()
        (was_good_enough, reason) = appraisal_method(self)
        if not was_good_enough:
            self.execute_self_modifying_code(reason)
        return appraisal_function
    def do_stuff(self):
        pass
    def execute_self_modifying_code(self, problem):
        deepjuju.kiss_booboo_better(self, problem)

Note that the default for appraisal_function will now be a function and not a method, even though is_silly_enough will be bound as a class method once the class is created (at the end of the code).

This means that

>>> SillyWalk.is_silly_enough
<unbound method SillyWalk.is_silly_enough>

but

>>> SillyWalk.walk.im_func.func_defaults[0] # the default argument to .walk
<function is_silly_enough at 0x0000000002212048>

And you can call is_silly_enough with a walk argument, or call a walk instance with .is_silly_enough().

If you really wanted is_silly_enough to be a static method, you could always add

    is_silly_enough = staticmethod(is_silly_enough)

anywhere after the definition of walk.

Jason R. Coombs
+1 That's pretty close to what I had in mind. I was thinking more about the general case, though, where `is_silly_walk` would, like a typical static method, not take an argument of the instance type. I guess I could do something like `is_silly_enough = staticmethod(functools.partial(is_silly_enough, None))` after the declaration of `walk` to provide the extraneous parameter so outside callers wouldn't have to. I guess the downside there is that the function signature would cease to serve as accurate documentation.
intuited
Sorry, that last comment didn't make sense. With the `staticmethod(...)` call placed after the declaration of the function that uses the static-to-be as a default parameter value, this works perfectly. Thanks.
intuited