Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Cecil Westerhof Newsgroups: comp.lang.python Subject: Re: Is this a good way to work with init and exception Date: Mon, 20 Jul 2015 01:27:46 +0200 Organization: Decebal Computing Lines: 45 Message-ID: <87vbdfwwwd.fsf@Equus.decebal.nl> References: <87380kzb8b.fsf@Equus.decebal.nl> <87r3o4xfhx.fsf@Equus.decebal.nl> <87io9gx8tb.fsf@Equus.decebal.nl> <87615fyem2.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="528adfd6ad074c92fdc6a7f8fb9e23d8"; logging-data="30955"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+3AxXkWizvfkhhwaja/iz/IHjgJL6Id3c=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) X-Homepage: http://www.decebal.nl/ Cancel-Lock: sha1:ag/KeA4XQH0zFL74mQcuO2Xpy3I= sha1:BN/gT6gJ3VXLa03KLnvcqYFFLFM= Xref: csiph.com comp.lang.python:94177 On Monday 20 Jul 2015 00:40 CEST, Chris Angelico wrote: > On Mon, Jul 20, 2015 at 8:19 AM, Cecil Westerhof wrote: >>> If two modules import the same module, they get two references to >>> that same module, not two separate module instances. Since your >>> parameters appear only to affect the initialization itself, this >>> is not likely to be a problem (it's not like you'll need to >>> authenticate with two different sets of credentials, for >>> instance), but it will mean that the second one will import an >>> already-initialized module. That's why I suggested the try_init >>> function which would quietly return an immediate success if the >>> module had already been initialized. But if this isn't going to be >>> an issue, then your code's fine. >> >> Good to know. I would expect two different instances. >> >> I agree that in my case it would not be a problem, but I put the >> code on GitHub: >> https://github.com/CecilWesterhof/PythonLibrary/blob/master/twitterDecebal.py >> I should do my best to circumvent nasty surprises for users of the >> code. Someone else could use several Twitter accounts at the same >> time. Is there a way to do this? > > Does the instantiation of Core() involve authentication? Is it > possible to call Core() more than once and use different accounts? > Your send_message() takes an account identifier, so it might be you > don't need separate accounts. But if, just very occasionally, you do > need multiple, here's a possible design style: Have init() return > the Core as well as stashing it in _core, and then have > send_message() take an optional keyword argument (in 3.x, > keyword-only) to choose a different core. That way, it'll by default > use the most recently initialized core, but you can create multiple > and manage them yourself if you so choose. (Obviously you'd use > reinit_allowed=True for all the initializations.) You are right: core is a general initialisation, so in this case nothing to worry about. :-D When I write something where it could make a difference, I should use your tip. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof