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


Groups > comp.lang.python > #72922

Re: How to use imported function to get current globals

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.stack.nl!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.017
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'resulting': 0.04; 'attributes': 0.09; 'subject:How': 0.10; 'def': 0.12; 'dict': 0.16; 'globals': 0.16; "module's": 0.16; 'modules,': 0.16; 'parameter:': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'module': 0.19; 'import': 0.22; 'print': 0.22; 'pass': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'code': 0.31; 'though.': 0.31; 'probably': 0.32; 'objects': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'interact': 0.36; 'should': 0.36; 'easily': 0.37; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'around.': 0.60; 'first': 0.61; 'more': 0.64; 'here': 0.66; 'evaluate': 0.72; 'subject:get': 0.81
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=2WjqPBlK5p1AzwDn3QbK2obZdlctbIy1tQfWAy6ng+4=; b=d5pvB6NylyyXSuIHIQpiEmbwcgfOrtLVtBJszH/tldsMgSu7fmopE39LhFJXR3fi6r drHB9iHoqnCA7h6ZrV2ZkcU0FzKklnDLGm1HVREL6Qy06cUVHBaf93xzSfdAxZEva2hS Yk+x7lk+m3wRFU/wxkw9tEjDB0agnFz/029ez7/aypx/N2k1wvnzhdWVeNscMnUzZaMB WMMamfbB3/U1G6T2G4auAkdjxNu/uYW4sFQ8P/GkbAxa3BCY6Z94Rsdbyqp3QlUSsugr kOH70p1+ls7xTFg2xyW4DkfdVD03uiCbHaT8fSqa87VKTs+ahJdOlTo+lXfCvJJjDF2/ A8IQ==
X-Received by 10.236.179.69 with SMTP id g45mr723954yhm.81.1402163906375; Sat, 07 Jun 2014 10:58:26 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <602B90F0-E7FE-4887-ADA1-981FD4179EC7@gmail.com>
References <602B90F0-E7FE-4887-ADA1-981FD4179EC7@gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sat, 7 Jun 2014 11:57:46 -0600
Subject Re: How to use imported function to get current globals
To Python <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
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>
Newsgroups comp.lang.python
Message-ID <mailman.10855.1402163915.18130.python-list@python.org> (permalink)
Lines 25
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1402163915 news.xs4all.nl 2892 [2001:888:2000:d::a6]:47200
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:72922

Show key headers only | View raw


On Sat, Jun 7, 2014 at 11:40 AM, 1989lzhh <1989lzhh@gmail.com> wrote:
> Here is the code
> m1.py
> def f():
>     print globals()
>
> m2.py
> from m1 import f
> f()# how to get current module's globals?

Evaluate globals() in the current module and pass the resulting dict
in as a parameter:

# m1.py
def f(globals):
    print globals

# m2.py
from m1 import f
f(globals())

There's a code smell here, though.  If your function really needs to
interact with globals from other modules, then those should probably
not be globals in the first place.  More likely they should be
attributes of objects that can be easily passed around.

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


Thread

Re: How to use imported function to get current globals Ian Kelly <ian.g.kelly@gmail.com> - 2014-06-07 11:57 -0600

csiph-web