SIngleton from __defaults__
Dave Angel
davea at davea.name
Thu Jan 23 11:56:33 EST 2014
Johannes Schneider <johannes.schneider at galileo-press.de> Wrote in
message:
> On 22.01.2014 20:18, Ned Batchelder wrote:
>> On 1/22/14 11:37 AM, Asaf Las wrote:
>> Chris is right here, too: modules are themselves singletons, no matter
>> how many times you import them, they are only executed once, and the
>> same module object is provided for each import.
>
> I'm not sure, if this is the whole truth.
>
> think about this example:
>
> cat bla.py
> a = 10
>
> cat foo.py
> from bla import a
>
> def stuff():
> return a
>
> cat bar.py
> from foo import stuff
> print stuff()
> a = 5
> print stuff()
>
> from bla import *
> print a
>
> python bar.py
> 10
> 10
> 10
>
> here the a is coming from bla and is known in the global namespace. But
> the value differs in stuff() and before/after the import statement. So
> the instance of the module differs -> it cannot be a singelton.
>
You're using 3 different variables here, each global to its own
module. If you really want to access the same object, you need to
reference it as bla.a. And ditch the from deal.
A from x import y. statement produces a new binding to the same
object. But since the object in your example is immutable, the
only way it can seem to change is by rebinding. If several names
are bound to the same object, rebinding one has no effect on the
others.
--
DaveA
More information about the Python-list
mailing list