Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #86260
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Subject | Re: Best practice: Sharing object between different objects |
| Date | 2015-02-23 20:13 +0000 |
| References | <aacac55a-6779-4ad3-96f4-5332ff36a365@googlegroups.com> <mcfqeb$5tb$1@dont-email.me> <mailman.19095.1424717125.18130.python-list@python.org> <ac52606e-f85f-4e94-8dac-6556e28821be@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.19099.1424722435.18130.python-list@python.org> (permalink) |
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 "<stdin>", line 1, in <module> > 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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Best practice: Sharing object between different objects pfranken85@gmail.com - 2015-02-21 04:15 -0800
Re: Best practice: Sharing object between different objects Dave Angel <davea@davea.name> - 2015-02-21 09:28 -0500
Re: Best practice: Sharing object between different objects Paul Rubin <no.email@nospam.invalid> - 2015-02-21 09:18 -0800
Re: Best practice: Sharing object between different objects Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-02-23 18:10 +0000
Re: Best practice: Sharing object between different objects Michael Torrie <torriem@gmail.com> - 2015-02-23 11:36 -0700
Re: Best practice: Sharing object between different objects sohcahtoa82@gmail.com - 2015-02-23 12:02 -0800
Re: Best practice: Sharing object between different objects Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-23 20:13 +0000
Re: Best practice: Sharing object between different objects Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-23 13:39 -0700
Re: Best practice: Sharing object between different objects Michael Torrie <torriem@gmail.com> - 2015-02-23 14:10 -0700
Re: Best practice: Sharing object between different objects Chris Angelico <rosuav@gmail.com> - 2015-02-24 11:14 +1100
csiph-web