Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed2a.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.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'api.': 0.05; 'class,': 0.07; 'def': 0.12; 'changes': 0.15; '"from': 0.16; '@property': 0.16; 'binding,': 0.16; 'singleton': 0.16; 'subject:?)': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'module': 0.19; 'implementing': 0.19; '>>>': 0.22; 'import': 0.22; 'mon,': 0.24; 'header:In-Reply- To:1': 0.27; 'michael': 0.29; 'skip:p 30': 0.29; 'am,': 0.29; 'properties': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'reflected': 0.31; 'class': 0.32; 'another': 0.32; 'subject: (': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'needed': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'itself': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'real': 0.63; 'mar': 0.68; '2015': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=hqP/lTYqgrTJ/HToKLFOXpIcxaYYr6cv698EZMtTuLc=; b=rY07MO8b5Z1yfqNj4wu35WKpYUDxuFHgn27yr7UVPuinKEWFRpX1Xp/7HfJ80g4Wdg Dgy2GhiwzUtga2FMulNKwO/bk68Bvb3rWTqA3/4CGffPyWMBdhgnsf9AU4PG976w8U9p T2e7UpqFWMgMoovwoEgZG0VAVI+NEaMDsIHpLOfv6OxUWAijFWImsGvVGN4ti3m7qU2a kB8m6K4fjvMCUX52e10bC5xDuLF+0u3JCrxnpfWZsY23+lU4MkYQa1ZlSumeuf/bMSUe M5D4zuK08Zr6NBSUoMVMaKlcYMIWd4qCUTagghv402c53S101U6BsyMpK86xI2Zzb49b ZDiw== X-Received: by 10.70.5.3 with SMTP id o3mr22539766pdo.17.1425315145353; Mon, 02 Mar 2015 08:52:25 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <54F488D0.2010906@gmail.com> References: <13v4falb4odnhtvss4qdatnn16sgiv5pgd@4ax.com> <54F488D0.2010906@gmail.com> From: Ian Kelly Date: Mon, 2 Mar 2015 09:51:45 -0700 Subject: Re: suggestions for functional style (singleton pattern?) To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.19 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: 25 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1425315154 news.xs4all.nl 2935 [2001:888:2000:d::a6]:49218 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86762 On Mon, Mar 2, 2015 at 8:59 AM, Michael Torrie wrote: > Now I don't know of any way of implementing class-style properties on a > module as you can do in a class, but if that were needed you would write > a standard class and instantiate a singleton inside the module itself > perhaps. This works, although I'm not sure that I would do it in real code. >>> class PropertyModule(types.ModuleType): ... @property ... def parrot(self): ... return "Norwegian Blue" ... >>> sys.modules['parrot_sketch'] = PropertyModule('parrot_sketch') >>> import parrot_sketch >>> parrot_sketch.parrot 'Norwegian Blue' Note that if the user does "from parrot_sketch import parrot", then the property is evaluated at import time and any changes won't be reflected in the local binding, which might cause some surprises. Also, this isn't really a singleton any longer since the user might just create another instance of the class, but you can hide the class and present just the module as the API.