Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!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.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'yet.': 0.04; '(except': 0.07; 'initialize': 0.07; 'subject:Question': 0.07; 'terms,': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; 'assume': 0.14; 'itself.': 0.14; 'thread': 0.14; 'algorithmic': 0.16; 'bits.': 0.16; 'called.': 0.16; 'cleaned': 0.16; 'example).': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'garbage': 0.16; 'imported.': 0.16; 'lambda': 0.16; 'module?': 0.16; 'only)': 0.16; 'readability': 0.16; 'readable': 0.16; 'subject:access': 0.16; 'subject:lifetime': 0.16; 'subject:object': 0.16; 'value"': 0.16; 'all.': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'module': 0.19; 'trying': 0.19; 'else,': 0.19; 'value.': 0.19; 'code,': 0.22; 'rules': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'module,': 0.24; 'string,': 0.24; 'decide': 0.24; 'cc:2**0': 0.24; '15,': 0.26; 'references': 0.26; '(for': 0.26; 'header:In-Reply-To:1': 0.27; 'idea': 0.28; 'function': 0.29; '[1]': 0.29; 'generally': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'too.': 0.31; '(unless': 0.31; 'coded': 0.31; 'loading': 0.31; 'object.': 0.31; "they'll": 0.31; 'another': 0.32; 'are:': 0.33; "i'd": 0.34; 'could': 0.34; 'anywhere': 0.35; 'connection': 0.35; 'created': 0.35; 'problem.': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'accessible': 0.36; 'functions.': 0.36; 'object,': 0.36; 'doing': 0.36; "didn't": 0.36; 'useful': 0.36; "i'll": 0.36; 'so,': 0.37; 'las': 0.37; 'two': 0.37; 'performance': 0.37; 'needed': 0.38; 'whatever': 0.38; 'pm,': 0.38; 'rather': 0.38; 'that,': 0.38; 'anything': 0.39; 'how': 0.40; 'ensure': 0.60; 'even': 0.60; 'skip:u 10': 0.60; 'experts': 0.60; 'till': 0.61; 'new': 0.61; 'simply': 0.61; 'simple': 0.61; "you're": 0.61; 'back': 0.62; 'save': 0.62; 'times': 0.62; "you'll": 0.62; "you've": 0.63; 'name': 0.63; 'more': 0.64; 'worth': 0.66; 'six': 0.68; 'guaranteed': 0.75; 'gain': 0.79; 'again?': 0.84; 'faster.': 0.84; 'guarantee.': 0.84; 'shadows': 0.84; 'technically': 0.84; 'ten,': 0.84; 'twelve': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=O4mv//tP4/0CCYrR27sM7b6xQ6/MatRG5U/MG+OirTQ=; b=ZhPtBHF2npmfR8qRDLnTMDeJBle9iLVv5cE+IxQ5v5ZBlT3ZAGmLfl7BtaN3KcEiN7 e1KQm6G5MxhKbLqVLhPgTVrh9ySHh6nKbXSoMy0ucRy+D17eZfSLRN1of4cpA59ZrSq9 vkoocKO86fMzAcBi0IffSII21gTrH5WMJBfk8Awf1bRJ2pWiD1Z3MAd+JM3g+6LdQSNY n2RZ6irkY1ij7N9wOhV8TDsXglemIgVkEF20JbEH4wvqY5CFEha+PbJQg9bIMfR4kzEi sMw0EuDEzy9obHmtVwQhzLWWQitl8OrF3MSH/fayx9Lt0YiG7A+oLlJkWJmwKaAmcitE g0NA== MIME-Version: 1.0 X-Received: by 10.66.26.176 with SMTP id m16mr2626569pag.142.1389791143675; Wed, 15 Jan 2014 05:05:43 -0800 (PST) In-Reply-To: <30012227-c0f5-4bbe-9e6c-d333cd5682a2@googlegroups.com> References: <30012227-c0f5-4bbe-9e6c-d333cd5682a2@googlegroups.com> Date: Thu, 16 Jan 2014 00:05:43 +1100 Subject: Re: Question about object lifetime and access From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389791512 news.xs4all.nl 2909 [2001:888:2000:d::a6]:58688 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63981 On Wed, Jan 15, 2014 at 11:13 PM, Asaf Las wrote: > Questions are: > - what is the lifetime for global object (p in this example). > - will the p always have value it got during module loading > - if new thread will be created will p be accessible to it > - if p is accessible to new thread will new thread initialize p value again? > - is it guaranteed to have valid p content (set to "module is loaded") whenever application() function is called. > - under what condition p is cleaned by gc. Your global p is actually exactly the same as the things you imported. In both cases, you have a module-level name bound to some object. So long as that name references that object, the object won't be garbage collected, and from anywhere in the module, you can reference that name and you'll get that object. (Unless you have a local that shadows it. I'll assume you're not doing that.) How do you go about creating threads? Is it after initializing the module? If so, they'll share the same p and the same object that it's pointing to - nothing will be reinitialized. As long as you don't change what's in p, it'll have the same value ([1] - handwave) whenever application() is called. That's a guarantee. For your lambda functions, you could simply make them module-level functions. You could then give them useful names, too. But decide based on code readability rather than questions of performance. At this stage, you have no idea what's going to be fast or slow - wait till you have a program that's not fast enough, and then *profile it* to find the slow bits. Unless you're doing that, you're completely wasting your time trying to make something faster. Start with readable, idiomatic code, code that you could come back to in six months and be confident of understanding. Do whatever it takes to ensure that, and let performance take care of itself. Nine times out of ten, you won't even have a problem. In the past twelve months, I can think of exactly *one* time when I needed to improve an app's performance after I'd coded it the readable way, and there was just one part of the code that needed to be tweaked. (And it was more of an algorithmic change than anything else, so it didn't much hurt readability.) Remember the two rules of code optimization: 1. Don't. 2. (For experts only) Don't yet. Follow those and you'll save more time than you would gain by micro-optimizing. And your time is worth more than the computer's. ChrisA [1] Technically p doesn't "have a value" at all. It's a name that's bound to some object. You can rebind it to another object, you can mutate the object it's bound to (except that you've bound it to a string, which is immutable), or you can sever the connection (with 'del p'), but in simple terms, it's generally "near enough" to say that p has a value.