On Fri, Apr 8, 2011 at 2:16 PM, Gregory Crosswhite
<[email protected]> wrote:
> Okay, so should I interpret your response as meaning that I should assume
> that people use setter methods, and if scripts don't and this causes
> problems then this will teach people that they should use setter methods?
>  :-)   I ask because my other potential option for implementing a system for
> implementing listeners is to add a hack using something like
> __getattr__/__setattr__ to monitor the fields for changes.

It is bad style to reach into another class for any reason, but see below.

Several getters/setters do more than just get & set.

As for the listener pattern, the proper approach, ironically, is for
your plugin to monkey-patch any methods that you want to change.  It's
ironic because doing so makes the plugin intimately aware of the
patched class, but this is in practice very safe.  Indeed, if there
are problems, you simply disable your plugin.

The great LeoUser used this approach for his emacs-related plugins,
which no longer exist because I spent a year integrating them fully
into Leo.

Monkey-patching is very easy: just use g.funcToMethod.  Here it is:

QQQQQ
@ The following is taken from page 188 of the Python Cookbook.

The following method allows you to add a function as a method of any class. That
is, it converts the function to a method of the class. The method just added is
available instantly to all existing instances of the class, and to all instances
created in the future.

The function's first argument should be self.

The newly created method has the same name as the function unless the optional
name argument is supplied, in which case that name is used as the method name.
@c

def funcToMethod(f,theClass,name=None):

    setattr(theClass,name or f.__name__,f)
QQQQQ

HTH.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/leo-editor?hl=en.

Reply via email to