Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.freenet.ag!news2.euro.net!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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'way:': 0.05; 'subject:Python': 0.05; 'happily': 0.07; 'python': 0.08; 'def': 0.13; 'anymore.': 0.16; 'assembly,': 0.16; 'aye': 0.16; 'comma,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'handy': 0.16; 'lambda': 0.16; 'logic.': 0.16; 'magic.': 0.16; "wouldn't": 0.17; 'wrote:': 0.18; '>>>': 0.18; 'this?': 0.19; 'maybe': 0.21; 'dec': 0.22; 'header:In-Reply-To:1': 0.22; 'statement': 0.23; 'consist': 0.24; 'keyword': 0.24; 'code': 0.25; 'helpful': 0.26; "i'm": 0.26; 'function': 0.27; '(this': 0.28; 'skip:( 40': 0.28; 'message-id:@mail.gmail.com': 0.28; 'pm,': 0.29; 'example': 0.29; 'decorators': 0.30; 'logic': 0.30; 'does': 0.32; 'thu,': 0.32; 'implement': 0.32; "isn't": 0.33; 'there': 0.33; 'it?': 0.33; 'done': 0.34; 'to:addr:python-list': 0.34; 'someone': 0.34; '(including': 0.34; 'nested': 0.34; 'skip:@ 10': 0.34; 'however,': 0.36; 'received:209.85.161': 0.36; 'uses': 0.36; 'skip:" 10': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'could': 0.37; 'doing': 0.38; 'using': 0.38; 'received:209.85': 0.38; 'why': 0.39; "it's": 0.40; 'missing': 0.40; 'received:209': 0.40; 'to:addr:python.org': 0.40; 'once': 0.60; 'more': 0.61; 'type': 0.61; '2011': 0.61; 'your': 0.61; 'high': 0.67; 'subject:!!!': 0.76; 'care,': 0.77; '10:22': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=/kAJ5hZKCKOtVJrGvmp7jdo38b6nTpI3PAzvUXE7Sw0=; b=bBhMjURd7T8gD5FEovPZll0ZHj/Q/TbvA7qfLb5bX4uWqgLQmphM0dmcHheJ9yKurA TJYWPce6yQVeVPDCMQkE+K67OCnkwbjYckKziu0e3aCYjpfsdCpQUAj6kE8mZuZK4bfC McKHcfJ863bLeJdPNOcP1zxFLS+JcsKti00A4= MIME-Version: 1.0 In-Reply-To: References: <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> Date: Thu, 8 Dec 2011 22:43:12 +1100 Subject: Re: I love the decorator in Python!!! From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 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: , Newsgroups: comp.lang.python Message-ID: Lines: 45 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1323344595 news.xs4all.nl 6915 [2001:888:2000:d::a6]:48164 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:16823 On Thu, Dec 8, 2011 at 10:22 PM, K.-Michael Aye wrote: > I am still perplexed about decorators though, am happily using Python for > many years without them, but maybe i am missing something? > For example in the above case, if I want the names attached to each other > with a comma, why wouldn't I just create a function doing exactly this? Why > would I first write a single name generator and then decorate it so that I > never can get single names anymore (this is the case, isn't it? Once > decorated, I can not get the original behaviour of the function anymore. The example given is a toy. It's hardly useful. However, there are a number of handy uses for decorators; mostly, they consist of giving a single simple keyword to a complicated set of logic. One example is the @classmethod and @staticmethod decorators - the code to implement them could be uglier than nested inline assembly, but you don't have to care, because you just type "@staticmethod" in front of your def statement and it does its magic. Here's a handy trick that I'm sure someone has done in a more sophisticated way: def trace(func): if debugmode: return lambda *a,**ka: (print(">"+func.__name__),func(*a,**ka),print("<"+func.__name__))[1] return func Then you put @trace in front of all your functions, and if debugmode is False, nothing will be done - but set it to true, and you get console output at the entry and exit of each function. >>> @trace def test(x): print("Test! "+x) return 5 >>> test("asdf") >test Test! asdf