Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #86756
| Path | csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <torriem+gmail@torriefamily.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.002 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'languages,': 0.04; 'patterns': 0.04; 'attribute': 0.07; 'class,': 0.07; 'debugging': 0.07; 'alter': 0.09; 'ambiguity': 0.09; 'imported': 0.09; 'namespace': 0.09; 'oop': 0.09; 'variable,': 0.09; '"from': 0.16; '"global"': 0.16; 'defined.': 0.16; 'from:addr:torriem': 0.16; 'from:name:michael torrie': 0.16; 'globals': 0.16; "module's": 0.16; 'namespace,': 0.16; 'path.': 0.16; 'rarely': 0.16; 'scope,': 0.16; 'singleton': 0.16; 'statements,': 0.16; 'subject:?)': 0.16; 'wrote:': 0.18; 'discussion': 0.18; 'variable': 0.18; 'module': 0.19; 'implementing': 0.19; 'import': 0.22; 'saying': 0.22; 'header:User-Agent:1': 0.23; 'config': 0.24; 'logical': 0.24; 'module,': 0.24; 'first,': 0.26; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'michael': 0.29; 'feature': 0.29; 'am,': 0.29; 'properties': 0.29; "doesn't": 0.30; 'cool': 0.30; 'errors': 0.30; 'evil': 0.31; 'maintains': 0.31; 'raised': 0.31; 'class': 0.32; 'used,': 0.33; 'could': 0.34; 'problem': 0.35; 'subject: (': 0.35; "can't": 0.35; 'but': 0.35; 'module.': 0.36; 'should': 0.36; 'easily': 0.37; 'sometimes': 0.38; 'message-id:@gmail.com': 0.38; 'problems': 0.38; 'needed': 0.38; 'to:addr:python-list': 0.38; 'does': 0.39; 'itself': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'users': 0.40; 'tell': 0.60; 'simply': 0.61; 'name': 0.63; 'refer': 0.63; 'become': 0.64; 'charset:windows-1252': 0.65; 'details': 0.65; 'here': 0.66; 'between': 0.67; 'covers': 0.68; 'sharing': 0.69; 'business': 0.70; 'helping': 0.70; '(global': 0.84; 'glance': 0.84; 'protects': 0.84; 'states,': 0.91; 'state.': 0.95 |
| X-Virus-Scanned | amavisd-new at torriefamily.org |
| Date | Mon, 02 Mar 2015 08:59:12 -0700 |
| From | Michael Torrie <torriem@gmail.com> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: suggestions for functional style (singleton pattern?) |
| References | <WXrIw.14066$uP4.9692@fx20.iad> <13v4falb4odnhtvss4qdatnn16sgiv5pgd@4ax.com> <jAwIw.631640$rq4.16649@fx27.iad> <mailman.2.1425186322.29956.python-list@python.org> <md1dep$s0t$1@speranza.aioe.org> |
| In-Reply-To | <md1dep$s0t$1@speranza.aioe.org> |
| Content-Type | text/plain; charset=windows-1252 |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.19 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.50.1425311965.13471.python-list@python.org> (permalink) |
| Lines | 43 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1425311966 news.xs4all.nl 2908 [2001:888:2000:d::a6]:53696 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:86756 |
Show key headers only | View raw
On 03/02/2015 03:19 AM, Fabien wrote: > On 01.03.2015 06:05, Michael Torrie wrote: >> A module*is* a singleton pattern, particularly one >> that maintains state. I use sometimes use this feature for sharing >> config and other data between other modules (global state when it's >> required). > > I do this too, after some helping recommendations I got from this > discussion group. I find it neat to use a module for sharing config > states, but I always wondered: does this pattern fall into the "globals > are devil" category? In traditional languages, globals were considered evil in part because of the ambiguity they raised (a problem of scope, really). In a function if you see a reference to a variable, and if globals were used, you then have confusion over whether the variable is local or global (could be both if one covers the other). And globals could be set from anywhere. Debugging and logical patterns become a lot harder with this ambiguity and errors are introduced. Modules don't have the same problems when used as a store for globals for several reasons. First, because they have a namespace associated with them. You can tell at a glance which name in your function is a reference to this "global" module. Now if you did do a "from module import *" then you can't tell as easily what is coming from the module and what is locally defined. But the cool thing here is if you rebind a variable imported from a module in your local namespace, it actually doesn't change the "global" variable (attribute). Instead it just rebinds your local name. So if you want to change an attribute in the module, you have to refer to it via the module.attribute path. Secondly, one should rarely write to a module's namespace from outside that module (though you can). Instead treat the module in an OOP fashion, and write business methods to alter the module's state in a specific way. This encapsulation protects and insulates the module's users from implementation details that could change from time to time. 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. As with all broad statements, saying globals are evil is simply not always true.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
suggestions for functional style (singleton pattern?) yves@zioup.com - 2015-02-28 16:12 -0700
Re: suggestions for functional style (singleton pattern?) Michael Torrie <torriem@gmail.com> - 2015-02-28 19:19 -0700
Re: suggestions for functional style (singleton pattern?) yves@zioup.com - 2015-02-28 21:11 -0700
Re: suggestions for functional style (singleton pattern?) Michael Torrie <torriem@gmail.com> - 2015-02-28 22:14 -0700
Re: suggestions for functional style (singleton pattern?) Mario Figueiredo <marfig@gmail.com> - 2015-03-01 04:45 +0100
Re: suggestions for functional style (singleton pattern?) yves@zioup.com - 2015-02-28 21:29 -0700
Re: suggestions for functional style (singleton pattern?) Michael Torrie <torriem@gmail.com> - 2015-02-28 22:05 -0700
Re: suggestions for functional style (singleton pattern?) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-02 02:55 +1100
Re: suggestions for functional style (singleton pattern?) Fabien <fabien.maussion@gmail.com> - 2015-03-02 11:19 +0100
Re: suggestions for functional style (singleton pattern?) Mario Figueiredo <marfig@gmail.com> - 2015-03-02 11:31 +0100
Re: suggestions for functional style (singleton pattern?) Michael Torrie <torriem@gmail.com> - 2015-03-02 08:59 -0700
Re: suggestions for functional style (singleton pattern?) Ian Kelly <ian.g.kelly@gmail.com> - 2015-03-02 09:51 -0700
Re: suggestions for functional style (singleton pattern?) Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2015-03-01 19:00 +1300
Re: suggestions for functional style (singleton pattern?) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-02 02:04 +1100
Re: suggestions for functional style (singleton pattern?) Mario Figueiredo <marfig@gmail.com> - 2015-03-01 19:20 +0100
Re: suggestions for functional style (singleton pattern?) Paul Rubin <no.email@nospam.invalid> - 2015-02-28 22:23 -0800
csiph-web