Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!204.52.135.9.MISMATCH!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'source,': 0.04; 'yet.': 0.04; '"""': 0.07; 'attribute': 0.07; 'method.': 0.07; '*args,': 0.09; 'exec': 0.09; 'namespace': 0.09; 'subject:Getting': 0.09; 'to:addr:comp.lang.python': 0.09; 'works.': 0.09; 'cc:addr:python- list': 0.10; 'def': 0.11; '"""\\': 0.16; '**kwargs)': 0.16; '**kwargs):': 0.16; '24,': 0.16; 'cmd': 0.16; 'mean,': 0.16; 'name)': 0.16; 'name):': 0.16; 'quits': 0.16; 'wrote:': 0.17; 'working.': 0.18; 'solution.': 0.18; 'simpler': 0.22; 'header:In- Reply-To:1': 0.23; 'import': 0.24; 'skip:_ 20': 0.24; 'print': 0.25; 'header:User-Agent:1': 0.26; 'cc:2**0': 0.27; 'raise': 0.27; 'cc:no real name:2**0': 0.28; "doesn't": 0.28; 'skip:" 20': 0.28; 'cc:addr:python.org': 0.29; 'instance': 0.29; 'class': 0.30; 'skip:_ 10': 0.31; 'points': 0.32; 'could': 0.33; 'received:google.com': 0.34; 'received:209.85': 0.35; 'goes': 0.35; 'there': 0.35; 'but': 0.36; 'received:209.85.216': 0.37; 'subject:with': 0.37; 'subject:: ': 0.37; 'received:209': 0.37; 'level': 0.39; 'method': 0.39; '(that': 0.63; 'skip:n 10': 0.63; 'otten': 0.84; 'received:209.85.216.184': 0.84 Newsgroups: comp.lang.python Date: Mon, 25 Jun 2012 13:04:32 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=76.115.184.65; posting-account=yMP7kQoAAAAXsyJyvL-3qYVslbdYOSQP References: <9d0c01f4-4430-4a45-8776-20d8dede9e14@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 76.115.184.65 MIME-Version: 1.0 Subject: Re: Getting lazy with decorators From: Josh English To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 59 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1340654681 news.xs4all.nl 6868 [2001:888:2000:d::a6]:50821 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24443 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?