Is there a reason not to do this?

Ron Garret rNOSPAMon at flownet.com
Fri Dec 1 12:53:41 EST 2006


In article <1164963978.876893.129800 at 79g2000cws.googlegroups.com>,
 "Michele Simionato" <michele.simionato at gmail.com> wrote:

> Ron Garret wrote:
> > One of the things I find annoying about Python is that when you make a
> > change to a method definition that change is not reflected in existing
> > instances of a class (because you're really defining a new class when
> > you reload a class definition, not actually redefining it).  So I came
> > up with this programming style:
> >
> > def defmethod(cls):
> >   return lambda (func): type.__setattr__(cls, func.func_name, func)
> 
> Why not just ``return lambda func: setattr(cls, func.func_name, func)``
> ?

Because I'm an idiot.  (i.e. yes, that is obviously the right way to do 
it.)

> The only thing I don't like is that all your
> functions/methods will end up begin 'None'.
>
> I'd rather to be able to use
> the help, so I would write
> 
> def defmethod(cls):
>     def decorator(func):
>         setattr(cls, func.func_name, func)
>         return func
>     return decorator
> 
> @defmethod(C)
> def m1(self, x):pass
> 
> help(m1)
> 
> 
> BTW, for people with a Lisp background I recommend using IPython with
> emacs  and the
> ipython.el mode. It is pretty good, even if not comparable to Slime.
> 
>                                      Michele Simionato

Good tips.  Thanks!

rg



More information about the Python-list mailing list