Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.01; 'arguments': 0.05; 'attributes': 0.05; 'compiler': 0.07; 'definitions': 0.07; 'function,': 0.07; 'imports': 0.07; 'python': 0.08; "(i'm": 0.09; '__name__': 0.09; 'portions': 0.09; 'question:': 0.09; 'wrote:': 0.15; "'__main__':": 0.16; 'compiling,': 0.16; 'f()': 0.16; 'subject:behavior': 0.16; 'classes,': 0.16; 'cc:addr:python-list': 0.16; 'pm,': 0.16; 'def': 0.16; 'wrap': 0.19; 'language': 0.20; 'cc:2**0': 0.21; 'cc:no real name:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'trying': 0.23; 'code': 0.24; 'bruce': 0.25; 'statement': 0.25; 'function': 0.26; 'tried': 0.27; "i'm": 0.27; 'somebody': 0.28; 'cc:addr:python.org': 0.30; 'classes': 0.30; 'definition': 0.30; 'completed.': 0.30; 'confused': 0.30; 'decorators': 0.30; 'class': 0.31; 'print': 0.32; "won't": 0.32; 'done,': 0.32; 'it.': 0.33; 'header:User-Agent:1': 0.34; 'there': 0.34; 'normally': 0.34; 'skip:# 10': 0.34; 'things': 0.34; 'example,': 0.35; 'uses': 0.35; "isn't": 0.35; 'anything': 0.37; 'unless': 0.37; 'some': 0.37; 'but': 0.37; 'could': 0.37; 'subject:: ': 0.38; 'think': 0.38; 'run': 0.39; 'case': 0.39; 'finished': 0.39; 'might': 0.39; 'happen': 0.62; 'closed': 0.62; 'below': 0.62; 'prove': 0.64; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; '"just': 0.84; '02:59': 0.84; 'different.': 0.84; 'time"': 0.84; 'received:172.16.1': 0.91; 'way)': 0.91 Date: Fri, 22 Jul 2011 20:05:01 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Thunderbird/3.1.11 MIME-Version: 1.0 To: "mhearne808[insert-at-sign-here]gmail[insert-dot-here]com" Subject: Re: Decorator behavior References: <8a7d158b-bc27-442c-9886-1a704d0c52e1@a11g2000yqm.googlegroups.com> In-Reply-To: <8a7d158b-bc27-442c-9886-1a704d0c52e1@a11g2000yqm.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:2cCZf4Wi+cZvj+3AEafGyXeugCPm9pG7link7ZuRdpn z7oeHIg0gU54r1+etHRYJJCNr0cLCSm88krTTZ6R+IXkL8fqBD y8BJbKPZsV/7zm055cCsqR0KUtFeKWohLOrw/yJ+kY3Ggd7CoQ 3B8bqPwFs4T8OMi9m3BrhXwIKkUsXuKBZ6AlD3uz6EHO7yWKYF NRCRL7ufRF3HCUo+1DsDw== Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: davea@ieee.org 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: 56 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311379507 news.xs4all.nl 23919 [2001:888:2000:d::a6]:60875 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10159 On 01/-10/-28163 02:59 PM, mhearne808[insert-at-sign-here]gmail[insert-dot-here]com wrote: > I am just trying to wrap my head around decorators in Python, and I'm > confused about some behavior I'm seeing. Run the code below (slightly > adapted from a Bruce Eckel article), and I get the following output: > > inside myDecorator.__init__() > inside aFunction() > Finished decorating aFunction() > inside myDecorator.__call__() > > My question: Why isn't the first print statement in "__main__" the > first line of code executed? Is aFunction() not closed somehow? > > #!/usr/bin/env python > > class myDecorator(object): > def __init__(self, f): > print "inside myDecorator.__init__()" > f() # Prove that function definition has completed > > def __call__(self): > print "inside myDecorator.__call__()" > > @myDecorator > def aFunction(): > print "inside aFunction()" > > if __name__ == '__main__': > print "Finished decorating aFunction()" > aFunction() > classes and functions and decorators have some portions that execute when they occur, long before anybody "calls" them. (I'm sure there are other examples; one might consider imports the same way) In the case of classes, anything outside of the method definitions will happen before the class definition is completed. For example, class attributes happen at that time. For functions/methods, default arguments are evaluated at the definition time. So if the default value makes a call, the call will happen at that time. Function decorators execute right after the corresponding function definition is built. Such decorators won't normally call the function, but as you notice, if you do call it, it will execute. When you think about it, these behaviors are the only reasonable way these things could be done, unless the compiler tried to do some "just in time" compiling, not really building the code till somebody uses it. And that would make the language a lot different. DaveA