Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24443

Re: Getting lazy with decorators

Newsgroups comp.lang.python
Date 2012-06-25 13:04 -0700
References <9d0c01f4-4430-4a45-8776-20d8dede9e14@googlegroups.com> <mailman.1442.1340525279.4697.python-list@python.org>
Subject Re: Getting lazy with decorators
From Josh English <Joshua.R.English@gmail.com>
Message-ID <mailman.1496.1340654681.4697.python-list@python.org> (permalink)

Show all headers | View raw


On Sunday, June 24, 2012 1:07:45 AM UTC-7, Peter Otten wrote:
> 
> You cannot access a class instance because even the class itself doesn't 
> exist yet. You could get hold of the class namespace with sys._getframe(),
> 
> def add_help(f):
>     exec """\
> def help_%s(self):
>     f = getattr(self, %r)
>     self.show_help(f)
> """ % (f.__name__[3:], f.__name__) in sys._getframe(1).f_locals
>     return f
> 
> but here's a simpler approach:
> 
> import cmd
> 
> def add_help(f):
>     def help(self):
>         self.show_help(f)
>     f.help = help
>     return f
> 
> 
> class BaseCmd(cmd.Cmd):
>     def __init__(self, *args, **kwargs):
>         cmd.Cmd.__init__(self, *args, **kwargs)
> 
>     def show_help(self, func):
>         print "\n".join((line.strip() for line in 
> func.__doc__.splitlines()))
> 
>     def __getattr__(self, name):
>         if name.startswith("help_"):
>             helpfunc = getattr(self, "do_" + name[5:]).help
>             setattr(self.__class__, name, helpfunc)
>             return getattr(self, name)
>         raise AttributeError
> 
>     @add_help
>     def do_done(self, line):
>         """done
>         Quits this and goes to higher level or quits the application.
>         I mean, what else do you expect?
>         """
>         return True
> 
> if __name__=='__main__':
>     c = BaseCmd()
>     c.cmdloop()


Okay. If I understand this, you are adding a help attribute to the class method. The help attribute is itself a function. 

There is nothing in the documentation (that I have found) that points to this solution. Even after reading the do_help method in the cmd.Cmd source, I don't see this as working.

Yet it works.

How?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Getting lazy with decorators Josh English <Joshua.R.English@gmail.com> - 2012-06-23 18:58 -0700
  Re: Getting lazy with decorators Peter Otten <__peter__@web.de> - 2012-06-24 10:07 +0200
    Re: Getting lazy with decorators Josh English <Joshua.R.English@gmail.com> - 2012-06-25 13:04 -0700
      Re: Getting lazy with decorators Peter Otten <__peter__@web.de> - 2012-06-26 08:57 +0200
        Re: Getting lazy with decorators Josh English <Joshua.R.English@gmail.com> - 2012-06-27 16:09 -0700
        Re: Getting lazy with decorators Josh English <Joshua.R.English@gmail.com> - 2012-06-27 16:09 -0700
      Re: Getting lazy with decorators Peter Otten <__peter__@web.de> - 2012-06-26 09:03 +0200
    Re: Getting lazy with decorators Josh English <Joshua.R.English@gmail.com> - 2012-06-25 13:04 -0700
  Re: Getting lazy with decorators "Stefan H. Holek" <stefan@epy.co.at> - 2012-06-24 12:44 +0200

csiph-web