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


Groups > comp.lang.python > #110728

Re: Sharing package data across files

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From John Pote <johnpote@jptechnical.co.uk>
Newsgroups comp.lang.python
Subject Re: Sharing package data across files
Date Tue, 28 Jun 2016 23:07:17 +0100
Lines 83
Message-ID <mailman.95.1467156001.2358.python-list@python.org> (permalink)
References <919dae31-1f29-4278-bdad-d3129aa17b64@googlegroups.com> <179a807b-95cf-460c-b971-8b2ee5405d9d@googlegroups.com> <b96d015b-a692-b4c7-bcec-967de5c8fb72@jptechnical.co.uk>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de 1xgqtl94Kvm3831R8C2PEwWy5mjfSZf/LofqYMkRotkQ==
Return-Path <johnpote@jptechnical.co.uk>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'skip:[ 20': 0.03; 'defines': 0.07; 'foo,': 0.09; 'mutable': 0.09; 'nameerror:': 0.09; 'obj': 0.09; 'semantics': 0.09; 'simplified': 0.09; 'skip:[ 30': 0.09; 'subject:files': 0.09; 'example:': 0.10; 'def': 0.13; 'importing': 0.15; '2016': 0.16; '__init__.py': 0.16; 'did.': 0.16; 'exposes': 0.16; 'funcs': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'later': 0.16; 'string': 0.17; '>>>': 0.20; 'all,': 0.20; '"",': 0.22; 'affected': 0.22; 'object.': 0.22; 'trying': 0.22; 'defined': 0.23; 'seems': 0.23; 'references': 0.23; 'split': 0.23; 'thanks,': 0.24; 'import': 0.24; '(most': 0.24; 'thus': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'example': 0.26; 'correct': 0.28; 'function': 0.28; 'ray': 0.29; "i'm": 0.30; 'print': 0.30; 'code': 0.30; 'writes': 0.30; 'changed': 0.33; 'problem': 0.33; 'michael': 0.33; 'foo': 0.33; 'traceback': 0.33; 'structure': 0.34; 'file': 0.34; 'but': 0.36; 'assigned': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'setting': 0.37; 'thought': 0.37; 'wrong': 0.38; 'files': 0.38; 'does': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'future': 0.60; 'your': 0.60; 'share': 0.61; 'john': 0.61; 'skip:u 10': 0.61; 'email addr:gmail.com': 0.62; 'charset:windows-1252': 0.62; 'here': 0.66; 'state.': 0.72; 'integrated': 0.72; '>from': 0.76; 'obvious': 0.76; "'foo'": 0.84; 'received:91.103': 0.84; 'reside': 0.91; 'received:2': 0.93; 'scott': 0.93
DKIM-Signature v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=jptechnical.co.uk; s=default; h=Content-Transfer-Encoding:Content-Type: In-Reply-To:MIME-Version:Date:Message-ID:From:References:To:Subject:Sender: Reply-To:Cc:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help: List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=UUjPUI1iuGlffTYBmVB/byVt48ukFD8i7UMZXSfLphs=; b=PxDZqw0fXa5Sa1KH5Xr/JmdlKl 98/9rXNSHgruV5MRwF3ukocX9WUF426/p2zWPn1D9aZfzpU2lMOUYgu8O5v2ZaDstkQE3SlIKFHap Ar4NgmH8qqTi8RL5n1YsV/OhO;
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:45.0) Gecko/20100101 Thunderbird/45.1.1
In-Reply-To <179a807b-95cf-460c-b971-8b2ee5405d9d@googlegroups.com>
X-OutGoing-Spam-Status No, score=-2.9
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - heather.dataflame.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - jptechnical.co.uk
X-Get-Message-Sender-Via heather.dataflame.com: authenticated_id: johnpote@jptechnical.co.uk
X-Authenticated-Sender heather.dataflame.com: johnpote@jptechnical.co.uk
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID <b96d015b-a692-b4c7-bcec-967de5c8fb72@jptechnical.co.uk>
X-Mailman-Original-References <919dae31-1f29-4278-bdad-d3129aa17b64@googlegroups.com> <179a807b-95cf-460c-b971-8b2ee5405d9d@googlegroups.com>
Xref csiph.com comp.lang.python:110728

Show key headers only | View raw



On 28/06/2016 20:55, zackbaker@gmail.com wrote:
> On Tuesday, June 28, 2016 at 1:17:23 PM UTC-6, scott...@gmail.com wrote:
>> I'm trying to create a package in which the constituent files share some state.  Apparently, I don't understand scopes, namespaces, and package semantics as well as I thought I did.  Here's the directory structure for a simplified example:
>>
>>     example/
>>       __init__.py
>>       vars.py
>>       funcs.py
>>
>> vars.py defines a single variable:
>>
>>      foo = 123
>>
>> funcs.py defines a function that reads and writes that variable:
>>
>>      def bar():
>>          global foo
>>          foo += 1
>>          return foo
>>
>> __init__.py exposes both of those to the caller:
>>
>>      from vars import foo
>>      from funcs import bar
>>
>> Alas, it seems the bar function does not reside in the same global scope as the foo variable:
>>
>>      >>> from example import foo, bar
>>      >>> foo
>>      123
>>      >>> bar()
>>      Traceback (most recent call last):
>>        File "<stdin>", line 1, in <module>
>>        File "example/funcs.py", line 3, in bar
>>          foo += 1
>>      NameError: global name 'foo' is not defined
>>
>> How can I make the example package work like one integrated module even though in reality it's split across multiple files?
>>
>> Thanks,
>> -- Scott
> This problem of references is addressed in:
> http://stackoverflow.com/questions/710551/import-module-or-from-module-import
>
> >From Michael Ray Lovett:
> For example, if I do this in module a:
>
> from foo import bar
> bar = "oranges"
>
> No code outside of a will see bar as "oranges" because my setting of bar merely affected the name "bar" inside module a, it did not "reach into" the foo module object and update its "bar".
Correct me if I'm wrong but is not the above only true if bar has been 
assigned to and thus references an imutable object? In your example the 
string "oranges".
If bar has been assigned to a mutable object in module foo then every 
module importing via "from foo import bar" will all import the name bar 
pointing to the same mutable object. If this mutable obj is changed via 
bar in one module then every other module importing bar will also see 
the change.
eg
In module foo:
     bar = ["apples","bananas","grapes"]

In module bar1
     from foo import bar
     bar[0] = "oranges"

In module barx at some later time
     from foo import bar
     ...
     print bar    #prints ["oranges","bananas","grapes"]

If my understanding here is correct then this would be a good case for 
never directly writing to a globle. Use getter()s and setter()s to make 
it obvious that any use of the setter() will be seen by all future calls 
to the getter().

Regards all,
John

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


Thread

Sharing package data across files scottpakin1@gmail.com - 2016-06-28 12:17 -0700
  Re: Sharing package data across files zackbaker@gmail.com - 2016-06-28 12:55 -0700
    Re: Sharing package data across files John Pote <johnpote@jptechnical.co.uk> - 2016-06-28 23:07 +0100
      Re: Sharing package data across files scottpakin1@gmail.com - 2016-06-28 17:02 -0700
      Re: Sharing package data across files Steven D'Aprano <steve@pearwood.info> - 2016-06-29 10:27 +1000
  Re: Sharing package data across files Michael Selik <michael.selik@gmail.com> - 2016-06-28 22:37 +0000
    Re: Sharing package data across files scottpakin1@gmail.com - 2016-06-28 16:51 -0700
      Re: Sharing package data across files Steven D'Aprano <steve@pearwood.info> - 2016-06-29 10:50 +1000
        Re: Sharing package data across files scottpakin1@gmail.com - 2016-06-28 18:24 -0700

csiph-web