Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'from:addr:yahoo.co.uk': 0.04; 'patterns': 0.04; 'attribute': 0.07; 'executes': 0.09; 'imported': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'uses.': 0.09; 'variables.': 0.09; 'yeah,': 0.09; 'python': 0.11; 'creates': 0.14; 'language.': 0.14; '23,': 0.16; 'attribute.': 0.16; 'behavior:': 0.16; 'constructor.': 0.16; 'docs.': 0.16; 'instantiated': 0.16; 'modules,': 0.16; 'nice:': 0.16; 'once.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:between': 0.16; 'subject:object': 0.16; 'variable.': 0.16; 'appropriate': 0.16; 'language': 0.16; 'wrote:': 0.18; 'module': 0.19; '(where': 0.19; 'slightly': 0.19; 'seems': 0.21; '>>>': 0.22; 'import': 0.22; 'reset': 0.22; 'header :User-Agent:1': 0.23; 'certainly': 0.24; 'module,': 0.24; 'tells': 0.24; 'define': 0.26; 'first,': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'michael': 0.29; 'am,': 0.29; 'program,': 0.31; 'code': 0.31; 'that.': 0.31; '"",': 0.31; '>>>>': 0.31; 'go.': 0.31; 'prints': 0.31; 'file': 0.32; 'class': 0.32; 'stuff': 0.32; 'up.': 0.33; '(most': 0.33; 'monday,': 0.33; 'projects.': 0.33; "i'd": 0.34; 'something': 0.35; 'but': 0.35; 'really': 0.36; 'doubt': 0.36; 'module.': 0.36; 'object,': 0.36; 'level': 0.37; 'to:addr:python-list': 0.38; 'fact': 0.38; 'rather': 0.38; 'anything': 0.39; 'recent': 0.39; 'expect': 0.39; 'does': 0.39; 'bad': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'easy': 0.60; 'deleting': 0.60; 'solve': 0.60; 'matter': 0.61; 'simple': 0.61; 'first': 0.61; 'times': 0.62; 'email addr:gmail.com': 0.63; 'refer': 0.63; 'our': 0.64; 'different': 0.65; 'charset:windows-1252': 0.65; 'between': 0.67; 'skip:r 30': 0.69; 'special': 0.74; 'behavior': 0.77; '2015': 0.84; 'dealt': 0.91; 'subject:Best': 0.91; 'state.': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Best practice: Sharing object between different objects Date: Mon, 23 Feb 2015 20:13:31 +0000 References: Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-92-24-222-48.ppp.as43234.net User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 In-Reply-To: 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: 76 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424722435 news.xs4all.nl 2852 [2001:888:2000:d::a6]:36802 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86260 On 23/02/2015 20:02, sohcahtoa82@gmail.com wrote: > On Monday, February 23, 2015 at 10:45:59 AM UTC-8, Michael Torrie wrote: >> On 02/23/2015 11:10 AM, Rob Gaddi wrote: >>> So I'd solve it with module level global variables. It's semi-frowned >>> upon on software stuff because it creates an unintentional shared state >>> between different modules, but you really HAVE a shared state, so it >>> needs to be dealt with. >> >> I would also do it with a module attribute. In my mind that's exactly >> the right way to go. >> >> But I disagree that it's frowned on or a bad thing. A module is a >> completely appropriate place to store state. In fact a module is an >> object, but a special one that can only be instantiated once. So as far >> as patterns go, a module is a singleton. Almost any time in Python you >> have something that you want to have exactly one instance of in your >> program, you don't want to define a class but rather just use a module. >> >> Any code in a module can be considered the constructor. It executes only >> once in your program when the module is first imported, no matter how >> many times its imported after that. >> >> I often use a module to store configuration that is shared across >> modules in my projects. > > That behavior always trips me up. My intuition tells me that every time you import a module, it re-runs the code in the module. So if I had a simple module named myModule.py that had a single line with `myInt = 1`, then I would *EXPECT* this behavior: > >>>> import myModule >>>> myModule.myInt > 1 >>>> myModule.myInt = 2 >>>> myModule.myInt > 2 >>>> import myModule >>>> myModule.myInt > 1 > > It seems slightly counter-intuitive, but the *ACTUAL* behavior (where the final line prints 2) does allow for something nice: Easy use of global variables. > > What's REALLY interesting is that this happens: > >>>> import myModule >>>> myModule.myInt > 1 >>>> myModule.myInt = 2 >>>> myModule.myInt > 2 >>>> del myModule >>>> import myModule >>>> myModule.myInt > 2 > > I would REALLY expect that deleting the module object and then re-importing would reset that variable. And then... > >>>> del myModule.myInt >>>> del myModule >>>> import myModule >>>> myModule.myInt > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'module' object has no attribute 'myInt' > > So yeah...modules are really only loaded once unless you use reload/imp.reload/importlib.reload. > > But yeah, it seems slightly counter-intuitive to me at first, but certainly has its uses. > Rule 1) Don't expect anything of Python, always read the docs. Rule 2) If in doubt always refer to rule 1) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence