​​
On Fri, May 8, 2015 at 2:45 PM, <[email protected]> wrote:

​> ​
Why do you need instance methods (using 'self')
​...you
​could use static class methods; and with these *one *decorator works
perfectly well:

That would work.  But I won't do it. I have never seen a class methods that
wasn't an invitation to trouble.  They are never needed, and they instantly
cause packaging difficulties.

Instead, rev a533869 contains the following:

1. helpCommands.cmd:

def cmd(name):
    '''Command decorator for the helpCommands class.'''
    return g.new_decorator(name,'helpCommands')

2. A common helper for all @cmd decorators:

def new_decorator(name,ivars=None):
    '''
    Return a new decorator for a command with the given name.
    Compute the class instance using the ivar string or list.
    '''
    def _decorator(func):
        if new_dispatch:
            def wrapper(event):
                c = event.get('c')
                self = g.ivars2instance(c,ivars)
                event = event.get('mb_event')
                    ### To be removed.
                # g.trace('self',self,'event',event,'func',func)
                func(self=self,event=event)
            wrapper.__name__ = 'wrapper-for-%s' % name
            wrapper.__doc__ = func.__doc__
            g.app.global_commands_dict[name]=wrapper
                # Put the *wrapper* into the global dict.
        return func
            # The decorator must return the func itself.
    return _decorator

3. A helper that recovers the instance object:

def ivars2instance(c,ivars):
    '''
    Return the instance of c given by ivars.
    ivars may be empty, a string, or a list of strings.
    A special case: ivars may be 'g', indicating the leoGlobals module.
    '''
    if not ivars:
        return c
    elif isString(ivars):
        return g if ivars == 'g' else getattr(c,ivars)
    else:
        obj = c
        for ivar in ivars:
            obj = getattr(obj,ivar)
            if not obj:
                g.trace('can not happen',ivars)
                return c
        return obj

I would *much *rather do this than use class methods anywhere in Leo.
YMMV, but you have little chance of changing my mind.  Yes, there is a bit
more code involved, but it is completely encapsulated.  In contrast,
static/class methods have global consequences.  I'm not going there.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to