dunder-docs (was Python is DOOMED! Again!)

Steven D'Aprano steve+comp.lang.python at pearwood.info
Tue Feb 3 20:57:12 EST 2015


Gregory Ewing wrote:

> Marko Rauhamaa wrote:
>> For (almost) all practical purposes, that is the Python way as well. If
>> object instantiation (conceptually) copied the class's methods into the
>> object's dict, you'd get the semantics I'm looking for.
> 
> If things worked the way you want, it would be
> impossible to store a function in an instance
> attribute and get it out again *without* it
> being treated as a method and getting 'self'
> added to its arguments. That would be a
> considerable nuisance when dealing with
> callbacks and the like.


Not impossible, just inconvenient. Assuming that the descriptor protocol 
runs on access to instance attributes as well as class attribute, the 
solution is to use a custom descriptor to return the bare function.

class function(object):
    def __init__(self, func):
        self.func = func
    def __get__(self, obj, cls=None):
        return self.func


instance.callback = function(mycallback)


Or you can possibly use staticmethod.



-- 
Steve




More information about the Python-list mailing list