Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #64644

Re: SIngleton from __defaults__

From Terry Reedy <tjreedy@udel.edu>
Subject Re: SIngleton from __defaults__
Date 2014-01-23 19:10 -0500
References (1 earlier) <mailman.5843.1390407541.18130.python-list@python.org> <d4053869-c668-4b5d-8e58-25b3650307df@googlegroups.com> <lbp5hf$vjs$1@ger.gmane.org> <52E11AD1.4080305@galileo-press.de> <lbrhf2$d4u$1@ger.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.5916.1390522230.18130.python-list@python.org> (permalink)

Show all headers | View raw


>   Johannes Schneider <johannes.schneider@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

This makes a a global in foo, bound to 10

>> def stuff():
>>           return a

This a refers to the global a in foo.

>> cat bar.py
>> from foo import stuff
>> print stuff()
>> a = 5

This bar.a is irrelevant to the behavior of stuff.

>> print stuff()
>>
>> from bla import *
>> print a
>>
>> python bar.py
>> 10

foo.a == 10

>> 10

foo.a == 10

>> 10

bla.a == 10

>> here the a is coming from bla

Twice

and is known in the global namespace.

There is no global namespace outside of modules.

>> the value differs in stuff()

No it does not.

and before/after the import statement.

foo.a does not change. bar.a is never used.

>> So the instance of the module differs

Nope. Each of the three module instances is constant. The bindings 
within each could change, but there are no rebinding in the code above.

-- 
Terry Jan Reedy

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

SIngleton from __defaults__ Asaf Las <roegltd@gmail.com> - 2014-01-22 08:07 -0800
  Re: SIngleton from __defaults__ Chris Angelico <rosuav@gmail.com> - 2014-01-23 03:18 +1100
    Re: SIngleton from __defaults__ Asaf Las <roegltd@gmail.com> - 2014-01-22 08:37 -0800
      Re: SIngleton from __defaults__ 88888 Dihedral <dihedral88888@gmail.com> - 2014-01-22 09:23 -0800
      Re: SIngleton from __defaults__ Ned Batchelder <ned@nedbatchelder.com> - 2014-01-22 14:18 -0500
        Re: SIngleton from __defaults__ Asaf Las <roegltd@gmail.com> - 2014-01-22 11:52 -0800
      Re: SIngleton from __defaults__ Johannes Schneider <johannes.schneider@galileo-press.de> - 2014-01-23 14:36 +0100
      Re: SIngleton from __defaults__ Dave Angel <davea@davea.name> - 2014-01-23 11:56 -0500
      Re: SIngleton from __defaults__ Terry Reedy <tjreedy@udel.edu> - 2014-01-23 19:10 -0500
      Re: SIngleton from __defaults__ Johannes Schneider <johannes.schneider@galileo-press.de> - 2014-01-24 09:20 +0100
    Re: SIngleton from __defaults__ Asaf Las <roegltd@gmail.com> - 2014-01-22 13:10 -0800
    Re: SIngleton from __defaults__ Asaf Las <roegltd@gmail.com> - 2014-01-22 14:34 -0800

csiph-web